Removing form from parent form in C #?

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.

+4
source share
5 answers

If the two forms do not have a relationship such as the parent dialog, you might just want to connect to the Disposed event in a subform to receive a notification when it closes.

 public partial class Form1 : Form { private Form2 _Form2; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (_Form2 != null) _Form2.Dispose(); _Form2 = new Form2(); _Form2.Disposed += delegate { _Form2.Dispose(); _Form2 = null; }; _Form2.Show(); } } 

Then all you have to do in Form2 is just close it:

 public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Close(); } } 
+3
source

MSDN docs for form utilization:

Dispose will be called automatically if the form is displayed using the Show method. If another method, for example, ShowDialog is being used, or the form has never been shown at all, you should call Dispose of yourself in your application.

A source

When closing against order:

When the form is closed, all resources created inside the object are closed and the form is located. You can prevent the form from closing when time starts by processing the closing event and setting the Cancel property. CancelEventArgs is passed as a parameter to your event handler. If the form you are closing is the starting form of your application, your application ends.

Two conditions when the form is not located at the close, when (1) it is part of the multi-document interface (MDI), and the form is not visible; and (2) you displayed the form using ShowDialog. In these cases, you need to call Dispose manually to mark all forms of garbage collection.

+7
source
  • You do not need to explicitly call Dispose on the form, the garbage collector will do this for you.

  • If you need something specific when Form2 "leaves", you can connect it to closing the form.

EDIT:

In Form2, click

by pressing the <
 this->Close(); 

This will close this instance of form2 (the form will disappear). If form1 still has a link to form2, then form 2 will not be collected by the garbage collector, and GC will not get rid of it.

If there is a reason form1 keeps a link to form2?

If so, form1 should handle closing2 event, then form1 can free its link to form2 (set it to null).

Now the GC will write form2 as a candidate to be assembled, it (perhaps more than one step) will call the Dispose method and free up Form2 memory.

+1
source

Are you not a true reader? There are many answers here.

Edit I want to close (dispose explicitly) form2, which is created in the class form1, when the on button Click the form2 button. This change should provide even more clarity.

If you use ShowDialog, then form2 returns when you call close (). So in Form1:

 private void button1_Click(object sender, System.EventArgs e) { Form2 oForm2 = new Form2(); oForm2.MyParentForm = this; if (oForm2.ShowDialog() == DialogResult.OK) { oForm2.Dispose(); //or oForm2.Close() what you want } } 

And then call Close () in form2.

+1
source

I did this once for my project to close one application and open another application.

  System.Threading.Thread newThread; Form1 frmNewForm = new Form1; newThread = new System.Threading.Thread(new System.Threading.ThreadStart(frmNewFormThread)); this.Close(); newThread.SetApartmentState(System.Threading.ApartmentState.STA); newThread.Start(); 

And add the following method. Your newThread.Start will call this method.

  public void frmNewFormThread)() { Application.Run(frmNewForm); } 
0
source

All Articles