I designed a form for login that named as form1, and have 2 more form form2,form3 form2 items showing in panel from form1 and what I want to do when I click the button in panel ( the item from form2 ) want to show form2 and hide form1 but the code isnt working
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
Form3 frm3 = new Form3();
frm1.Hide();
frm3.Show();
};
form3 is opening but form1 isnt hiding
CodePudding user response:
Its not hiding because you created a new instance for form1 which is already instantiated.
You must call the Hide()
method on the same instance used to call the Show()
method.
If you added this code inside form1 class ,then change it like this
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
this.Hide();
};
CodePudding user response:
You creating both forms in your codesnipped. Form1 is not the form you want to close, i think. frm1 is only another instance of Form1, but not the openend instance von Form1. You must have anywhere another instance of Form1. You must use the right referenz to the right instance.