btmach
btmach

Reputation: 375

Closing a form when other form is opening

The current problem is that the "mainScreen" shows up, but immediately closes, I have no idea why. This is the piece of code that handles the close and opening of the new form.

Edit: .this refers to Login.cs (sorry)

 if(templogin == true && permission.Equals("1"))
 {
                mainScreen.IsAdmin();
                this.Close();
                mainScreen.ShowDialog();
 }

Upvotes: 1

Views: 90

Answers (2)

Sinatr
Sinatr

Reputation: 22008

If you want to run 2 forms (switching from one to another), then you should do it in Main

Instead of

Application.Run(new StartupForm());

you'll have to use

var startup = new StartupForm();
startup.ShowDialog();
if(somecondition) // when StartupForm is closed and return something (or property is set, etc)
{
    var main = new MainScreen();
    main.ShowDialog();
}

You don't need Application.Run at all when using ShowDialog.

Upvotes: 1

Akash KC
Akash KC

Reputation: 16310

I think, you should not close the application(as login form seems main form while you started the application) before another form run: Do like this :

if(templogin == true && permission.Equals("1"))
 {
                this.Hide();
                mainScreen.IsAdmin();
                mainScreen.ShowDialog();
                this.Close();

 }

Upvotes: 5

Related Questions