tags:

views:

1153

answers:

4

In my main method, I start the main form as usual:

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());

In the main forms load thing I have the following, which will ask the user to log in and stuff.

        using (loginForm)
        {
            DialogResult r = loginForm.ShowDialog();
            switch (r)
            {
                case DialogResult.OK:
                    break;
                default:
                    Application.Exit();
                    return;

            }
        }

My problem is that the main form shows up in the background, and I want it to, well... not. until the login is ok. How should I do this? The Application.Run() method say it automatically shows the form. Is there an alternative way of starting the main form without showing it? Or do I have to set visible to false in the constructor of the main form, and then back to true when log in is done, or something similar? What is the recommended way of doing something like that? The login forms intention is like a combined splash screen and login. So first it loads and sets up some different things, and then it tells the user to login.

+1  A: 

The simplest would be to start the login form with Application.Run. When the login is ok and other stuff has been set just show the 'main' form.

Application.Run(new LoginForm());

and in login form on the OK button:

this.Visible = false;
this.ShowInTaskbar = false;
MainForm mainForm = new MainForm();
mainForm.Show();
Aleris
+6  A: 

Instead of:

Application.Run(new MainForm());

try:

LoginDialog // the class that handles login UI
  login = new LoginDialog ();

if (login.ShowDialog () == DialogResult.OK)
{
   // check credentials
   // if credentials OK
   Application.Run(new MainForm());
}

The ShowDialog method is a blocking call, the program will halt until the dialog is closed in response to user input - by pressing an 'OK' button or 'Cancel' button.

Skizz

Skizz
Oh now that's security! Hehehe...
Filip Ekberg
Obviously you need to add the credential check bit instead of just having a comment (incase you weren't being ironic).
Skizz
yeah, that was my initial thinking, but I read somewhere that running a different form first could cause problems somehow? Can't find where it was though... and don't know how much it was in that... anyways, you can display a form before Application.Run is called?
Svish
@Filip was that sarcasm, or what did you mean by that comment? Should I do something else? or was it something with Skizz answer?
Svish
@Svish This is fine. You're right that there will be problems showing non-modal windows before Application.Run has been called, but modal dialogs are fine because they create their own message loops.
Stu Mackellar
aha, ok. I have now used this solution, and seems to me at least that it is working great. Thank you for fast answer!
Svish
A: 

about a little sleep befor calling the mainForm? or a OnLoadComplet?

Polo
A: 

I liked Skizz answer, but if that doesn't help you, you can do it also in the event load of MainForm. Just put there you code to show the login dialog.

Bruno Costa