0

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();
    index.Show();
    this.Close();
}

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 :)

Community
  • 1
  • 1
Andrew De Forest
  • 6,829
  • 14
  • 39
  • 50

5 Answers5

2

Do it like:

static class Program
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  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)
     }
   }
  }
 }

//form1:
 public partial class Form1 : Form
 {  
  string userName; 
  public Form1(string _strUser)
  {   
   InitializeComponent();
   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!
  }
 }
Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
1

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.

Mario
  • 35,726
  • 5
  • 62
  • 78
1

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.

Community
  • 1
  • 1
denied66
  • 644
  • 7
  • 18
1

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.

Sogger
  • 15,962
  • 6
  • 43
  • 40
0

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

Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30