Use C # To open a new window and close the old window

Source: Internet
Author: User

I recently developed a membership point management system in practice. By the way, I studied how to close the old window after opening a new window. I checked a lot of information on the Internet and summarized it as follows in combination with my own experiments, if you have any mistakes, please correct them more.

1. the most widely used method on the Internet is the hiding method. The principle is to hide the old window and open a new window.
The specific code is as follows:

// Write the code in a proper position in the old window

// Create a NewForm window (NewForm is a self-defined Form)
NewForm fm = new NewForm ()

This. Hide (); // Hide the current window
Fm. Show (); // new window display

In this method, the old window will still occupy the memory, so if it is not good for the login window, but if the old window is the main window to be restored later, this method is very good, because you can close the new window and use the old one later. show () to restore the old window.
Therefore, this method is suitable for transferring information between the main window and the subwindow.

2. the login window is designed.
Defined in Program. cs as follows:

Static void Main ()
{
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );

// Create a Login window (Login is a custom Form)
Login Log = new Login ();

// Use the mode dialog box to display logs
Log. ShowDialog ();
 
// DialogResult is used to determine whether to return the parent form
If (Log. DialogResult = DialogResult. OK)
{
// Open the main form in the thread
Application. Run (new Main ());
}
}

In the login window, Log. DialogResult = DialogResult. OK is assigned after verification.
In this way, the logon window is closed and the main window is opened.

I still don't quite understand why it is so troublesome, but the general reason should be that "a main form must always exist, and all the forms created on it have its own, if you close it, the rest will naturally be closed ". I am also learning ~
You may wonder if the main window can appear directly, hide it in the constructor, and create a new login window on it. After the login is complete, the login window disappears, the main window is displayed again. It is probably feasible, but I tried it several times but it was not successful ......

Well, this is the depressing and confusing experience of beginners ......

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.