C # Opening a new form and closing another

In my program, I show the login form in the main form, as soon as the program detects a successful login, I use this:

MainForm Main = new MainForm(); Main.Show(); this.Hide(); 

This works fine, but there is a problem, the login form is still open, although it is hidden, so when the program is closed, the process still freezes, how can I stop this?

Sorry, forgot to add, use this.Close(); does not work and will completely close the program.

+8
c # winforms
source share
5 answers

You need to specify your MainForm when you view the application, and in the Load event handler of this form you request a login. In this case, you will have the application launched and Login to start:

Program.cs

  Application.Run(new MainForm()); 

MainForm.cs

  private void Form1_Load(object sender, EventArgs e) { LoginForm loginForm = new LoginForm(); if (loginForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // check login here } } 

PS Close completely closes your application if it is the main form of your application.

+8
source share

Try something else like this:

 this.Hide(); Main.ShowDialog(); this.Close(); 

You want to hide the login form before you open the dialog box, and then close the login form after closing the dialog.

Simply closing the Login dialog box will eventually end the application, so this is not a real solution, but you still want to hide the login.

Simply put, put things in the order you want them to go in, especially when working with message loops.

First you hide the login form.
You will then see the “Basic Form” dialog, but do not let the caller “ShowDialog ()" continue until the dialog closes.
Finally, once the dialog is closed, you close the login form, ending the application.

+10
source share

Change the main form of MainForm , and when the application starts, in the form of login to MainForm_Load as a dialog box so that they can not access the main form.

  • If you need to close the application from the login form, use Application.Exit(0);
  • If you do not want them to view the main view and override SetVisibilityCore and call it inside MainForm_Load .
+1
source share

You can use ApplicationContext.MainForm to specify the current main form for the application:

 static class Program { public static ApplicationContext Context { get; set; } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Context = new ApplicationContext(new LoginForm()); // pass Context instead of just new LoginForm() Application.Run(Context); } } 

then in the input handler:

 private void login_Click(object sender, EventArgs e) { Program.Context.MainForm = new MainForm(); // close login form this.Close(); // set up context to track main form instead of login Program.Context.MainForm.Show(); } 
+1
source share

What about this.Close() instead?

-one
source share

All Articles