Take the 2-minute tour ×

I have a Winforms application that starts up with a login screen. If the user’s login credentials are good, this method is called:

public void success()
    mainForm index = new mainForm();

I thought that this would open the mainForm and close the login form, however when I run this, it closes the whole application. Why is that? I’m pretty new to C# and OOP in general, so any tips are appreciated!

I tried doing something like this: Closing a form and then call another one

But I still got the same result.

Thanks :)

share|improve this question

5 Answers 5

up vote 2 down vote accepted

Do it like:

static class Program
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  static void Main()
    using (Login login = new Login())
     login.StartPosition = FormStartPosition.CenterScreen;
     if (login.ShowDialog() == DialogResult.OK)
      Application.Run(new Form1(login.strUserName)); //passing the userName to the constructor of form1 (see bellow)

 public partial class Form1 : Form
  string userName; 
  public Form1(string _strUser)
   userName = _userName; //a local variable of a form1 class has hold user`s name (userName - which u can call it from within the form1 class!
share|improve this answer
The code shows how to pass a variable (of username) from login form to main form. –  Mitja Bonca Apr 12 '12 at 15:38
Thank you for the code, I'm working on implementing it! One question, should userName = _userName actually be userName = _strUser? Otherwise, where should I be getting _userName from? –  Andrew De Forest Apr 12 '12 at 15:51
It can be any variable. This is a variable that I made up. –  Mitja Bonca Apr 13 '12 at 5:51

As explained in the other question, you can't close your application's main window (i.e. the window passed to Application.Run()).

To solve this, pass an instance of your mainForm to Application.Run() and let your mainForm show the login dialog upon opening.

share|improve this answer

You might want to move your login form in the Program.cs instead of setting it as the startup form. See more here: How can I close a login form and show the main form without my application closing?

Another solution would be to hide the login form after the user login successfully and open the new one and just use an event to close the login form (which will be hidden) when the main window closes.

share|improve this answer

The answers here explain some solutions, but to clarify the problem, see Application.Run Method (Form) on MSDN. It states:

This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application.

So, because you are starting with the login box, as soon as it closes, it fires ExitThread and closes the application. As I said, the other answers here illustrate several ways to avoid this issue like creating the login box directly in Main() or starting the main form and have it manage displaying the login box.

share|improve this answer

To answer on your question: It can be any variable. This is a variable that I made up. Where it comes from: it actually comes from Login form. There is a public property which has a private setter, and can only be set in Login form. As you can see from my code, I get it:

Application.Run(new Form1(login.strUserName)); 

login is a reference of Login form. So I only "transfter" strUserName varaible from Login form to Program (where it all started), and to Form1, so I can show it there. Why I have to tranfer it and why I cannot simply access to it from Form1? This is because when code comes to Form1, Login form is already disposed, and all members from Login are set to null (not accessible). You can see when I instanitated Login class, I did it in using block, so when code leaves it, class dispose it too.

BTW, Login form looks like:

class Login
   public string strUserName { get; private set; }

Hope it helps understanding. bye

share|improve this answer

Your Answer


