登录表单没有正确失去焦点

Rya*_*ant 6 .net c# forms login winforms

我有一个系统,其中主窗体是菜单,并在加载时弹出登录表单.最初它加载了菜单下面的登录表单,所以我使用this.topmost = true来使它出现在前面.(因为带到前面并且发送回来不起作用)

但是,如果用户然后单击其他内容(例如chrome),则根据最顶层的定义,它仍然位于z顺序的顶部.

我尝试使用deactivate事件,但这意味着在加载时它再次出现在菜单表单后面.

我怎么能阻止它加载到我的菜单表格后面,但是当它失去焦点时它会阻止它成为最顶层的?

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;
}
Run Code Online (Sandbox Code Playgroud)

在菜单表格中:

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    
}          
Run Code Online (Sandbox Code Playgroud)

Mar*_*all 2

尝试将登录的表单所有者属性设置为菜单表单。

来自上面的 MSDN 链接:

当一个窗体由另一个窗体拥有时,它会与所有者窗体一起关闭或隐藏。... 拥有的表单也永远不会显示在其所有者表单后面。您可以对窗口使用自有窗体,例如查找和替换窗口,当选择所有者窗体时,这些窗体不应消失。要确定父表单所拥有的表单,请使用 OwnedForms 属性