Login form does not lose focus correctly

I have a system in which the main form is the menu, and when loading the login form appears. He initially downloaded the registration form under the menu, so I used this.topmost = true to make it come to the fore. (how to bring to the front and send back does not work)

However, if the user then clicks on something else, say, chrome, then he still remains at the top of the z-order, by definition, at the very top.

I tried to use to deactivate the event, but this meant that when loading it again appears behind the menu form.

How can I stop him from loading behind my menu form, and yet, when he loses focus, stop him from the 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 form of a menu:

 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 } 
+6
source share
2 answers

Try customizing the Owner login form in the menu form.

Top MSDN link:

When a form belongs to another form, it closes or hides the form owner .... Own forms are also not displayed for their form owner . You can use your own forms for windows, such as searching and replacing windows that should not disappear when the owner form is selected. To define forms that belong to the parent form, use the OwnedForms Property

.

+2
source

Assuming this is a Win Forms application, try changing theForm.Show() to theForm.ShowModal()

0
source

Source: https://habr.com/ru/post/901902/


All Articles