Detailed introduction to C#'s method of closing a child window without releasing the child window object.

黄舟
Release: 2017-03-27 11:49:19
Original
1971 people have browsed it

The editor below will bring you an articleC#How to close a child window without releasing the child windowobject. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

During the debugging process of the online scanning camera, it is necessary to open a debugging interface to configure the position. After debugging, a common way is to save the debugging parameters and load them at the next startup. Another simple way is to run the program directly with this parameter. Therefore, in the latter case, the function that needs to be implemented is: even if the debugging window is closed, its window object is not released. The object of its debug window is not destroyed until its main window is closed.

1 Instantiate the sub-window in the main window

Instantiate the sub-window in the main window instead of instantiating the sub-window in the button window object.

Form2 f2 = new Form2();
Copy after login

2 Display the main window through buttons

What needs to be achieved in the button is the display of the window

private void Config_Click(object sender, EventArgs e)
    {
      f2.Show();
    }
Copy after login

3 Method to close the child window without releasing the child window object

After inquiry and verification, it is feasible to modify the Dispose method in the child window. Change as follows:

 protected override void Dispose(bool disposing)
    {
      Hide();
      //if (disposing && (components != null))
      //{
      //  components.Dispose();
      //}
      //base.Dispose(disposing);
    }
Copy after login

4 Destroy the child window object when the parent window is closed

Since the child window object needs to be destroyed when the parent window is closed, Therefore, add a call to the destruction function of the child window f2 in the closing action FormClosed of the parent window.

 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
      f2.Close();
    }
Copy after login

The closing functions added in the sub-window class are as follows:

 public void Close()
    {

      this.Dispose();

    }
Copy after login

The above is the detailed content of Detailed introduction to C#'s method of closing a child window without releasing the child window object.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template