I have a form that will open a new form when a single button is clicked (form1button). And on the child form, another 'form2button' button will appear. Now, if I click on this button form2, I will need to delete the new form2. But since the form2 object is created here in the form1 class method, I cannot destroy this object in the form2 class method (fom2buttonclick). So I used static to do my job, as in the following psuedo code.
Form1:
class Form1 : Form { static Form2 f2; public void Form1_buttonclick(object sender, EventArgs e) { f2 = new Form2(); } public void Disposef2() { f2.Dispose(); } }
Form2:
class Form2 : Form { public void Form2_buttonclick(object sender, EventArgs e) { Form1 f1 = new Form1(); f1.Disposef2(); } }
Is there any other better way to do this. The C # construct itself does not provide an alternative mechanism. I am new to C #. Please help me.
Edit
I want to close (explicitly forbid) the form2 object that is created in the form1 class when a button is clicked on form2. This change should provide additional clarity.
source share