Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login form not losing focus correctly

I have a system in which the main form is the menu, and it pops up a login form on loading. Originally it loaded the login form below the menu, so I used this.topmost = true to make it come to the front. (as bring to front and send to back did not work)

However, if the user then clicks on something else, say chrome, then it still stays at the top of the z-order, by definition of topmost.

I tried to use the deactivate event, but this meant that on load it once again comes up behind the menu form.

How could I stop it from loading behind my menu form, and yet when it loses focus stop it from being topmost?

private void login_Deactivate(object sender, EventArgs e)
{
     // do not want it to remain top most when the application is not in focus.
     this.TopMost = false;
}

In the Menu form:

private void Menu_Load(object sender, EventArgs e)
{
     openLogin()
}

private void openLogin()
{
     Cursor.Current = Cursors.WaitCursor;
     login theForm = new login(this);            
     this.Enabled = false;            
     theForm.Show();
     Cursor.Current = Cursors.Default;
     theForm.Activate(); 
     theForm.TopMost = true; // Make the login form display over the Menu    
}          
like image 303
Ryan Durrant Avatar asked Nov 19 '11 19:11

Ryan Durrant


1 Answers

Try setting Login's Form Owner property to the menu form.

From above MSDN link:

When a form is owned by another form, it is closed or hidden with the owner form. ... Owned forms are also never displayed behind their owner form. You can use owned forms for windows such as find and replace windows, which should not disappear when the owner form is selected. To determine the forms that are owned by a parent form, use the OwnedForms property

.

like image 113
Mark Hall Avatar answered Nov 05 '22 16:11

Mark Hall