我可以使用 MenuStrip 作为 ContextMenu 吗?
女士们先生们大家好!
我有一个 MenuStrip
并希望有一个具有相同结构的 ContextMenu
。 我可以将我的 MenuStrip
用作 ContextMenu
,或者使用 MenuStrip
中的数据源快速创建 ContextMenu
吗?
谢谢!
Hi Ladies and Gentlemans!
I have a MenuStrip
and want to have a ContextMenu
with same structure.
Can I use my MenuStrip
as ContextMenu
, or quick create ContextMenu
with DataSource from MenuStrip
?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为您可以将
MenuStrip
中的项目用作ContextMenuStrip
。我以前遇到过这个问题,我同意菜单应该是菜单,但它们显然是作为不同的控件实现的,因为它们在幕后需要稍微不同的功能。但是,您可以使用
ContextMenuStrip
作为MenuStrip
控件的下拉菜单。因此,您可以通过相反的方式来实现您正在寻找的行为。根据需要设计 ContextMenuStrip,然后将其分配给 您想要的MenuStrip
上的顶级ToolStripMenuItem
的DropDown
属性要显示的菜单。或者,您可以走很长的路,在设计器中创建两个不同的菜单,但将项目从一个菜单复制并粘贴到另一个菜单。这至少可以节省您设计两个看起来完全相同的菜单的时间。您甚至可以连接事件处理程序(例如,每个菜单项的 Click 事件)来处理两个菜单中的项目。例如,在 VB.NET 中,
Handles
关键字使这变得非常简单:I do not believe that you can use an item from a
MenuStrip
as aContextMenuStrip
. I've run into this before, and I agree that a menu should be a menu, but they're apparently implemented as different controls because they require slightly different functionality under the covers.However, you can use a
ContextMenuStrip
as a drop-down menu for theMenuStrip
control. So you can achieve the behavior you're looking for by doing it the other way around. Design theContextMenuStrip
however you want it, and then assign it to theDropDown
property for the top-levelToolStripMenuItem
on theMenuStrip
that you want that menu to be shown for.Or, you can go the long way and create two different menus in the Designer, but copy-and-paste the items from one menu to the other. This at least saves you the time of designing two menus to look exactly the same. You can even wire up the event handlers (say, for the
Click
events of each menu item) to handle the items from both menus. In VB.NET, for example, theHandles
keyword makes this extremely simple:如果您正在寻找简明扼要的简短内容,请回答:否。
If you are looking for a short, to the point, answer: No.