[英]Menu click causes form to “lose focus” and menu dropdown disappears
I have noticed some strange form interactions while using a ContextMenuStrip
or a MenuStrip
. 我注意到在使用ContextMenuStrip
或MenuStrip
时发生了一些奇怪的表单交互。 I don't really know what is causing it so the following should create a repeatable test for anyone looking into this: 我真的不知道是什么原因造成的,因此以下内容应该为任何关注此问题的人创建可重复的测试:
I've created two Windows Forms: Form1
and Form2
. 我创建了两个Windows窗体: Form1
和Form2
。 Both have a MenuStrip
added to them. 两者都添加了MenuStrip
。 Both are set to StartPosition
= CenterScreen
. 两者都设置为StartPosition
= CenterScreen
。
Form1
has a simple menu like this: Form2 > Open
. Form1
具有一个简单的菜单,如下所示: Form2 > Open
。 Clicking "Open" will launch Form2
. 单击“打开”将启动Form2
。
Form2
has a simple menu like this: Try to open > anotherTestMenuItem
Form2
具有一个简单的菜单,如下所示: Try to open > anotherTestMenuItem
Form1
opens. Form1
打开。 Form2
from Form1
(ie click Form2 > Open
). 从Form1
打开Form2
(即,单击Form2 > Open
)。 Form2
appears. Form2
出现。 Form2
, try to open the MenuStrip
(click Try to open
). 在Form2
,尝试打开MenuStrip
(单击“ Try to open
)。 Form1
will reappear over Form2
, although Form2
still has focus (you can see this if you move Form2
a little before trying to open the menu). Form1
将重新出现在Form2
,虽然Form2
仍然具有焦点(你可以看到这一点,如果你移动Form2
试图打开菜单有点过了)。 If I set Form2
's owner to Form1
, Form2
remains visible when you try Step 4, but the menu doesn't display the first time. 如果我将Form2
的所有者设置为Form1
,则在尝试执行步骤4时, Form2
仍然可见,但是菜单不会第一次显示。 All subsequent clicks seem fine. 以后所有的点击似乎都很好。
I noticed this when I tried opening a context menu (on a form opened by another form) and it would disappear immediately, but only the first time. 当我尝试打开一个上下文菜单(在另一个表单打开的表单上)时,我注意到了这一点,它会立即消失,但这只是第一次。 Every time thereafter it would open normally. 此后每次它将正常打开。
Does anyone have any ideas as to what is going on? 有人对发生的事情有任何想法吗?
Yes, this is a known bug in the RTM version of .NET 4.5. 是的,这是.NET 4.5的RTM版本中的一个已知错误。 This KB article mentions it: 此知识库文章提到了它:
When you click a menu item to open a child window in the application, interactions with the menu and child windows behave incorrectly. 当您单击菜单项以在应用程序中打开子窗口时,与菜单和子窗口的交互行为不正确。
For example, you may experience the following: 例如,您可能会遇到以下情况:
- When you open a shortcut menu in the child window, the main window form takes the focus. -在子窗口中打开快捷菜单时,主窗口窗体将成为焦点。
- You cannot use mnemonics to access a menu item. -您不能使用助记符访问菜单项。
As you can tell from the KB article, this bug was fixed quite a while ago. 正如您从知识库文章中看到的那样,此错误已在很早之前修复。 The bug fix was incorporated in a maintenance release first made available on January 8th of 2013. Be sure to allow Windows Update to deploy that update on your machine. 该错误修复程序包含在2013年1月8日首次发布的维护版本中。请确保允许Windows Update在您的计算机上部署该更新。 Or download it from here . 或从这里下载。
Check to see that you have: 检查您是否拥有:
"AutoValidate" set to "EnablePreventFocusChange" 将“自动验证”设置为“ EnablePreventFocusChange”
Make sure you do not call the Hide method for a menu or contextmenu during its item-click handler. 确保在菜单项或上下文菜单的项目单击处理程序中未调用其Hide方法。 I was in the habit of doing this during development to get a menu off the screen while I stepped through debugging code. 我习惯于在开发过程中执行此操作,以便在逐步调试代码时从屏幕上获得菜单。 However when I launched a secondary form from such a menu, the first right-click in the new form would cause the focus to revert to the primary form, sometimes even if I had left-clicked or typed something in the new form. 但是,当我从这样的菜单启动辅助表单时,即使在新表单中左键单击或键入某些内容时,第一次在新表单中单击鼠标右键也会使焦点恢复为主要表单。 I don't recall if I found this solution online somewhere or discovered it myself. 我不记得我是在某个地方在线找到该解决方案还是自己发现了它。 This may not be the same bug mentioned above, but this behavior still occurs in .NET 4.7. 这可能与上面提到的错误不同,但是在.NET 4.7中仍然会发生此行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.