Here is a small series for everyone to bring a C # implementation to close the child window without releasing the child Window object method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
In the debugging process of the online scanning camera, it is necessary to develop a debugging interface to configure the location. After debugging, a common way is to save the debug parameters and load the next time you start. Another simple way is to run the program directly using this parameter. Therefore, in the latter case, the function that needs to be implemented is that the Window object is not freed even if the debug window is closed. The object of its debug window is destroyed unless its main window is closed.
1 instantiating a child window in the main window
Instantiates a child window in the main window instead of instantiating the child window object in the button.
Form2 F2 = new Form2 ();
2 displaying the main window via a button
What you need to implement in the button is the display of the window
private void Config_click (object sender, EventArgs e) { F2. Show (); }
3 Ways to close child windows without releasing child window objects
It is possible to modify the Dispose method in the Subwindow by querying and empirically. The changes are as follows:
protected override void Dispose (bool disposing) { Hide (); if (disposing && (components = null)) //{ //Components . Dispose (); } //base. Dispose (disposing); }
4 Destroying a child window object when the parent window is closed
Because the child window object needs to be destroyed when the parent window is closed, the destroy function that calls the child window F2 is added in the close action formclosed of the parent window.
private void Form1_formclosed (object sender, Formclosedeventargs e) { F2. Close (); }
The closing function added in the child window class is as follows:
public void Close () {this . Dispose (); }