tags:

views:

151

answers:

5

I have encountered a problem in my application. I have two forms, one that is loaded when my application is started that asks for a password and other is shown when the user logs in with correct password.

How can I close the login form and have the user proceed to next form that is actual application?

Currently I hide the login form, but the requirement is to close the login form to prevent extra processing. However, when I close the login form my application exits and when I hide the login form but close the actual application form the login form remains open and prevents my application from closing because the login form is still running in background.

How can in fix this?

A: 

I would say simplest approach would just be to display the first form in the Load event of the second as a modal dialog. If the password auth fails you can call Close in the event handler and the second form will go away as well.

And obviously you would use the second form as the one passed to Application.Run in your Main() function.

tyranid
+3  A: 

The way I would typically handle this is have code similar to what follows in the main method;

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    if (PerformLogin())
    {           
        Application.Run(new MainForm());
    }
}

private static bool PerformLogin()
{
    using (LoginForm loginForm = new LoginForm())
    {
        if (loginForm.ShowDialog() == DialogResult.OK)
        {
            return AuthenticateUser(loginForm.UserName, loginForm.Password);
        }
        else
        {
            return false;
        }
    }
}

Since the login form is created and destroyed within the PerformLogin method, it does not remain in memory longer than necessary.

Fredrik Mörk
A: 

Even simpler - get into the main method. open the first form as DIALOG, initialize application, open second form with Application.Run. Finished.

TomTom
A: 

I presume, you have used

 LoginForm.Hide();

Use

 LoginForm.Close();

I may be wrong too... but just trying my luck...

The King
i explained i have tried this but it not work for me..
moon
A: 

I have similar problem: ... in Main() I call Application.Run(new LoginForm());

LoginForm contains ip address to connect to, login and passw. Inside LoginForm I call a method which sends data to a device on ip address and verifies the access. If access is granted, connection is established and this is the point where I need to close (this.Close())the LoginForm and open the main window(form) of an application.

I wanted to use an IF(LogingForm is succesfull) Application.Run(new MainWindow()) in Main() function ... but I can't get any value to use in IF() compare.

Do you have any idea how to solve this? Should I use the first login form as Dialog or as its now is OK?

tom