Link to home
Start Free TrialLog in
Avatar of SEinarsson
SEinarsson

asked on

Ways to call code after Form OnLoad is finished?

I have 2 forms, a Login window and a mainform. However it isn't the standard Login -> Mainform flow. When the program starts, the Mainform appears (with all functionality disabled) and the Login window appears ontop of the MainForm. When the user logs in, the Login form goes away, and the Mainform is enabled and active.

Currently, the way Im doing this, in Mainform_Onload it starts a Timer set to 100. When the timer pulses, it disables the timer and creates the Login and does ShowDialog(). If they enter correct login information, the Login form goes away and then Mainform is enabled.

I'm using a timer because if I create a Login instance during Mainform_Onload and ShowDialog() while still in the Onload, the Login page shows, but the Mainform never gets painted until the user is done with the Login screen.

Is there a way to do what I am doing *without* having to rely on a Timer? So far nothing I've done has gotten Mainform to show before it finishes the OnLoad method, and the only way I've found to delay the Login page until that has been done is by using a Timer.

-SEinarsson-


Avatar of Yurich
Yurich
Flag of New Zealand image

since you want to have your login screen as a modal dialog, the way you're currently using is prorably the one only. There could be a couple of different ways in the case of not modal dialog, including satring your login form in a separate thread, but since your loging logically must be modal, i don't see any other ways of doing it.

If you want to change your functionality, you can of course have a button "Login" in which case you wouldn't need any timer.

regards,
yurich
Huh, I think I have figured out something...

If you don't want to use a timer, you can imploy your mouse enter event of the form, in the following way:

// must have, otherwise will appear everytime you enter your mouse on to the form
bool logged = false;
private void Form1_MouseEnter(object sender, System.EventArgs e)
{
      if( !logged )
      {
            Login login = new Login();
            login.ShowDialog();
            logged = true;
      }
}

it will work fine but I can see a little problem already... You'll have to notify a user somehow that s/he has to point the mouse on something in order to log-in. Anyway, you can work out something starting from here ;)

regards,
Yurich
Avatar of jonvaughan
jonvaughan

Is this WebForm or WinForm ?
ASKER CERTIFIED SOLUTION
Avatar of jonvaughan
jonvaughan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that's right! just put that stuff in a constructor. Well done Jon, collect your points ;)
And why not ?
Avatar of SEinarsson

ASKER

This is WinForm.

Putting the Login in the Constructor of the Mainform, and the Mainform doesn't get shown on screen until after the user has logged in. I need the Mainform to be seen on-screen, with the Login window on top of it.

And the login screen has to appear automatically, I can't wait until the user mouse-enters the form.

-Sarkis-
actually it will... have you tried jon's code?? works just fine - the main form is shown at the same time as you login form, you can drop a couple of controls on it to see that's are properly drawn as well.
I had ran the code I was working with with the ShowDialog() in the first Form's constructor, and it didn't show. Tried it again just now and it works this time, I guess I did/copied something wrong the first time. It looks to be doing what I need now, thanks.

-SEinarsson-