How to start another main form in C #

First I show the login form. When the user enters the correct ID and password, I want to show another form and close the login form. The following is a way to run the login form.

static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FrmLogin()); } } 

Now that I want to show the main form, I call the dispose() method of the dispose() class, but the application ends immediately. My solution changes the visible property of the FrmLogin class to false , and I know this is wrong, suggest a way for this.

+4
source share
4 answers

You can display the login form as a dialog and, if you want to log in, you can run the main form as:

 static class Program { public static bool isValid = false; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); using (FrmLogin login = new FrmLogin()) { login.ShowDialog(); if (isValid) { Application.Run(new MainForm()); } } } } 

in your FrmLogin, check the user and set DialogResult as Ok . Here I did this on a button click event.

 private void btnLogin_Click(object sender, EventArgs e) { Program.isValid= true; // impliment this as method if(Program.isValid) { this.DialogResult = DialogResult.OK; // or this.Close(); } else { //else part code } } 
+1
source

How about you make the second APplication.Run after the login has completed;) Wait until it closes, sign in, then Application.Run for the second form, which is the MAIN form.

Btw., "FrmLogin" is a violation of the .NET name patterns. You seem to be an old VB hand (it was a sample from there). This should be LoginForm.

+2
source
 var loginForm = new LoginForm(); if(loginForm.ShowDialog() != dont_remember_see_intellisense_or_docs.OK) return; var mainForm = new MainForm(); Application.Run(mainForm); 

As a result, the loginform dialog box appears. In the login form, you must set the return value to the appropriate value (for example, OK) to show the main form

+1
source

To show another form myForm from FrmLogin, just call

 myForm window = new window() window.MDIparent = ParentForm; window.Show(); 
-2
source

All Articles