-1

I have 2 forms

form1 = login,
form2 = main 

I tried this to switch from form1 to form2

(new Form2()).Show();
this.Hide();

And everything work just fine but when I close the program is still showing in the process manager/ doesn't stop debugging automatically, so how to fix that?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
N3xT
  • 17
  • 5

2 Answers2

1

You need to add this line:

form2.Closed += (s, args) => Close();

So your code should be something like this:

Hide();
Form2 form2 = new Form2();
form2.Closed += (s, args) => Close();
form2.Show();

This will close Form1 when you close Form2. If the user presses X or ALT+F4 or RightClick -> Close on Form2, the Form2 and the hidden Form1 will be closed.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

I recommend a completely different and reliable method.

Login.cs

public partial class Login: Form
{
    public bool isLogin=false;//This should be public
    public Login()
    {
        InitializeComponent();
    }



    private void CheckUsernamePassword(string username,string password){
        if(username=="yourname" && password =="yourpass"){
           isLogin=true;
           this.close();
        }else{
           MessageBox.Show("Wrong username or password");
        }

     }
 }

Program.cs

//Remove this line
"Application.Run(new Form1());" replace

Login login = new Login();
Application.Run(login);
if(login.isLogin){// User logged
     Application.Run(new main());
}

This code will work very successfully now.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Adem Bulut
  • 11
  • 3