lundi 16 mars 2015

Is there a better way to show another form?

Whenever I need to show another Form, I always instantiate a new Form Object to show another Form (then hiding the current form).



/*Code in Form1*/

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();

this.Hide();
}


After I instantiated and showed the second Form, I want to dispose the previous form (to free up some memory usage) but it is not working on the main context form.



However, the Dispose() method is working on other WinForms which is not the main context form.




/*Code in Form2*/

private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Show();

this.Dispose();
}


Is it possible to instantiate a Form Object once only, then eventually to call/show it whenever I need it?


Aucun commentaire:

Enregistrer un commentaire