C # Close one window and open another window

Source: Internet
Author: User

In. in the net WinForm program, if the Form is directly started as the main window, the main window cannot be closed because it maintains a Windows message loop, once it is closed, the entire application is declared to end, so the newly opened window is automatically closed. Therefore, the window to be closed cannot be created using Application. Run.
To open another window while closing one window, follow these steps:

Declare a public bool variable in the first window and assign the value true:
For example, public bool closeflag = true;
Run the second window in the program class. The main code is as follows:
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
Form1 form1 = new Form1 ();
Form1.ShowDialog (); // mode window. Run Form1 first.
If (from1.closeflag = false)
{
Application. Run (new Form2 ());
}

Then, assign the closeflag value to false in the closed controls of Form1 (such as the button or something) and call this. close (); close the first window.

(Reference)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.