I would like to know how to remove elements from my windows forms using C #, I am currently doing an application with C # and winforms and in one of the parts of my application there is a window that has two winforms embedded inside a main winform, this is because there are two buttons that tell me what secondary winform should be called.
Image of the current window.
The elements that can be seen are inside a secondary winforms that is called inside the main winform.
Code where the secondary winform is inserted into the main one.
public Resguardo()
{
InitializeComponent();
Resguardo_Compra compra = new Resguardo_Compra();
compra.TopLevel = false;
compra.Location = new Point(0,50);
this.Controls.Add(compra);
compra.Show();
notSize();
}
This is done in the constructor and therefore when starting the application, it does it that way correctly, but now I want to click on the sale to change my winform for another one that believes in specific for the sales module of the same way (putting the secondary winform in the main one). So I thought about removing the secondary winform from purchase and then putting the winform for sale, but it did not work.
Replacement code:
private void btnVenta_Click(object sender, EventArgs e)
{
Resguardo_Compra compra = new Resguardo_Compra();
this.Controls.Remove(compra);
Resguardo_Venta venta = new Resguardo_Venta();
venta.TopLevel = false;
venta.Location = new Point(0, 50);
this.Controls.Add(venta);
venta.Show();
}
It is assumed that the two lines above should delete the first winform and those below show the second. Someone who knows how to make this form substitution.