C # Close the first Form window and open another new window method

Source: Internet
Author: User

C # Close the first Form window and open another new window method

Many people ask how to close a FORM and open another new Form. The most serious problem is that when performing login verification, You need to close yourself after successfully logging on to the first logon window, and then open a new window. I didn't expect this problem to happen in. Net, but there are still many solutions.
Typical
Form1 f = new Form1 ();
F. Show ();
This. Close ();
The entire program is closed at last.

1. The principle is to hide the old window and open a new 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.
After the task is completed in the logon window, the main window is opened.

Most of the above two methods are available on the Internet. In addition, we can see a method using the thread method, which has not been verified.
3. Open a new thread
Public static void ThreadProc ()
{
Application. Run (new Form ());
}

Private void button#click (object sender, EventArgs e)
{
System. Threading. Thread t = new System. Threading. Thread (new System. Threading. ThreadStart (ThreadProc ));
T. Start ();
}


C # When programming, we often encounter the problem of processing two or more windows. Take the logon window as an example. To enter the main window after successful login verification in the logon window, you need to close the logon window. this. close () is not allowed at this time. Because Program. cs is in

Static void Main ()
{
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
Application. Run (new Form1 ());
}

The Main function is the entry point of the program. When you call this in Form1. application. in Run (new Form1 (), Form1 ends, that is, the entire program ends, and the main window is closed accordingly. This is not what we want to see. There are two ways to close the logon window, and the main window is still running.

4. Hide the logon window first, close it in the main window, and close the logon window.

This method only hides the logon window and the main program is still running. Implementation Method:

Logon window code:

If (user. CheckIn ())
{
MainForm myform = new MainForm (this); // call the constructor with Parameters
Myform. Show ();
This. Hide ();
}
Else
{
MessageBox. Show ("incorrect user name or password ");
}

After successful logon, input the parameters of the logon window to the main window through the constructor, so that you can directly operate on the logon window in the main window.

Form1 loginform = null;
// Add a constructor with parameters in the Main Window
Public MainForm (Form1 myform)
{
This. loginform = myform;
InitializeComponent ();
}

Close the logon window after closing the main window.

Private void MainForm_FormClosed (object sender, FormClosedEventArgs e)
{
Loginform. Close ();
}

5. Set the main window program as the main running program,

Modify Program. cs

Application. Run (new MainForm ());

In this way, the main window program is run when the system starts. Then you can call the logon window when loading the main window.

Private void MainForm_Load (object sender, EventArgs e)
{
Form1 form = new Form1 ();
Form. ShowDialog ();
This. Close ();
}

The logon window code is:

If (user. CheckIn ())
{
This. Close ();
}
Else
{
MessageBox. Show ("incorrect user name or password ");
}

Reference: how to close the first Form window in c # And open another window in vs.net

Reference: C # open another window in one window and close it at the same time

Related Article

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.