防止ToolStripMenuItem上下文菜单从点击关闭 [英] Prevent ToolStripMenuItem context menu from closing on click

查看:520
本文介绍了防止ToolStripMenuItem上下文菜单从点击关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的上下文菜单保持在屏幕上,即使我点击其中的一个下拉项目(按shift键,虽然我不认为这是重要的问题)。您可以在Windows XP中看到一个示例,当您单击开始>所有程序>附件> [现在按下您的Shift键],然后单击Windows资源管理器...菜单保持状态。



这是一个C#应用程序,使用Winforms,开发机器是Windows 7,生产是XP,Vista或7。



工具条似乎有一个关闭事件;一个关闭一个。熟悉关闭事件的人将知道您可以设置取消标志,以防止控件关闭。 p>

此外,当我尝试从其点击事件或其关闭事件,它不工作。虽然这将是一个容忍的解决方法在立即,它不是生产。



任何建议或相关信息,非常感谢。



谢谢

解决方案

我有一个动态创建的子菜单,通过将其Parent DropDown菜单的AutoClose属性设置为False,点击此处:

  ParentMenu.DropDown.AutoClose = false; 

其中ParentMenu是一个 ToolStripMenuItem p>

使用DropDown的父 ToolStripDropDownMenu 的Closing事件通过设置取消标志来实现此功能并不是可行的解决方案是因为它在其两个父级菜单中的任何一个中导致不一致的显示/隐藏行为,并导致屏幕上意外的视觉伪影,以后通过代码隐藏时无法摆脱。它也似乎导致动态创建的菜单的父母的某些事件不再起火,例如其 MouseEnter 事件。



在这段经历中有趣的发现是,尽管Visual Studio 2010的智能感知列表中列出了一个 DropDown LostFocus 事件上下文菜单项;当将此事件添加到动态创建的菜单时,它不会被触发;这显然是一个已知的行为,提到这里


I'm trying to have my context menu stay on screen even after I click one of its dropdown items (when the shift key is pressed, though I don't think that matters to the issue). You can see an example of the behavior in Windows XP when you click on Start > All Programs > Accessories > [now hit your shift key] and click on Windows Explorer... The menu stays up.

It's a C# app, using Winforms, development machine is Windows 7, production is either XP, Vista or 7.

The toolstripmenuitem doesn't seem to have a closing event; only a closed one. Those familiar with a closing event will know that you can set a cancel flag to prevent the control from closing.

Also, when I try a workaround of remaking it visible from within either its click event or its closed event, it doesn't work. Although that would have been a tolerable workaround in the immediate, it is not for production.

Any suggestions or related info greately appreciated.

Thank you

解决方案

I was able to have a dynamically created submenu of my ContextMenu stay on screen upon being clicked by setting the AutoClose property of its Parent DropDown menu to "False" like this:

ParentMenu.DropDown.AutoClose = false;

where ParentMenu is a ToolStripMenuItem.

The use of the Closing event of the DropDown's Parent ToolStripDropDownMenu to acheive this by setting a "Cancel" flag was not a viable solution because it caused inconsistant showing/hiding behavior in either of its 2 levels of Parent menus as well as causing unexpected visual artifacts on screen that I could not get rid of when later being hidden through code. It also seemed to cause certain events of the dynamically created menu's Parents to no longer fire, such as its MouseEnter event.

An interesting find during this experience was that although Visual Studio 2010's intellisense lists a LostFocus event for a DropDown of a context menu item; when adding this event to dynamically created menus it does not get fired; this is apparently a known behavior as mentionned here:

这篇关于防止ToolStripMenuItem上下文菜单从点击关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆