Mainform is turned off after the maintenance

xiaoxiao2021-03-06  107

Mainform is turned off after the maintenance

Author: Luo Huitao

Winform programs are generally run from a Form's Static void main (), which is called Mainform. The general writing method of the main function is:

Static void main ()

{

Application.run (New Form1 ());

}

Such a write method has a problem: if you need to adjust another Form from Form1, keep Form1 cannot be turned off, such as the form of FORM2 in a button Click:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

(New form2 ()). show ();

}

You can see that two forms are displayed at the same time, if it is changed:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

(New form2 ()). show ();

THIS.CLOSE ();

}

So when you do this.close (), the entire application will terminate it, and Form2 will automatically turn off.

If you need to display FORM2, there is another way to change Form1.Close to Form1.hide, it is to change main to:

Static void main ()

{

(New form1 ()). Show ();

Application.run (); // Start the independent application thread

}

The this.close () in Form1 will not cause the application to terminate, and if you do not need Form1, you can really close to release some resources. However, the termination of the application should not be controlled elsewhere, such as in the form of form2:

Private void form2_closed (Object Sender, System.EventArgs E)

{

Application.exit ();

}

In multi-document applications, you often need to maintain a list of current active FORMs internally. When the last Form is closed, then call Application.exit.

About the Author:

Luo Shi Tao is Grapecity's technical director. He has worked with others with books such as "Made MS SQL Server 7.0" and "Accounting Computerization Practical Technology", and published the number of articles in the Computer World News. And taught in the Two Microsoft DevDays Technology Conference.

转载请注明原文地址:https://www.9cbs.com/read-103572.html

New Post(0)