我可以使用 MenuStrip 作为 ContextMenu 吗?

发布于 2024-10-02 06:04:07 字数 230 浏览 0 评论 0原文

女士们先生们大家好!

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有深☉意 2024-10-09 06:04:07

我不认为您可以将 MenuStrip 中的项目用作 ContextMenuStrip。我以前遇到过这个问题,我同意菜单应该是菜单,但它们显然是作为不同的控件实现的,因为它们在幕后需要稍微不同的功能。

但是,您可以使用ContextMenuStrip作为MenuStrip控件的下拉菜单。因此,您可以通过相反的方式来实现您正在寻找的行为。根据需要设计 ContextMenuStrip,然后将其分配给 您想要的 MenuStrip 上的顶级 ToolStripMenuItemDropDown 属性要显示的菜单。

或者,您可以走很长的路,在设计器中创建两个不同的菜单,但将项目从一个菜单复制并粘贴到另一个菜单。这至少可以节省您设计两个看起来完全相同的菜单的时间。您甚至可以连接事件处理程序(例如,每个菜单项的 Click 事件)来处理两个菜单中的项目。例如,在 VB.NET 中,Handles 关键字使这变得非常简单:

Private Sub Exit_Click(ByVal sender As Object, ByVal e As EventArgs) _
   Handles exitMenuItem.Click, exitContextMenuItem.Click
   Me.Close()
End Sub

I do not believe that you can use an item from a MenuStrip as a ContextMenuStrip. 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 the MenuStrip control. So you can achieve the behavior you're looking for by doing it the other way around. Design the ContextMenuStrip however you want it, and then assign it to the DropDown property for the top-level ToolStripMenuItem on the MenuStrip 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, the Handles keyword makes this extremely simple:

Private Sub Exit_Click(ByVal sender As Object, ByVal e As EventArgs) _
   Handles exitMenuItem.Click, exitContextMenuItem.Click
   Me.Close()
End Sub
别理我 2024-10-09 06:04:07

如果您正在寻找简明扼要的简短内容,请回答:

If you are looking for a short, to the point, answer: No.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文