如何将ContextMenuStrip添加到ToolStripDropDownButton [英] How to add a ContextMenuStrip to a ToolStripDropDownButton

查看:150
本文介绍了如何将ContextMenuStrip添加到ToolStripDropDownButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ToolStrip,它有各种下拉菜单(ToolStripDropDownButtons)。其中一些菜单是动态的 - 根据用户在其他地方执行的操作(例如文件菜单底部的MRU文件列表),以编程方式添加和删除项目。

I have a ToolStrip that has various drop down menus (ToolStripDropDownButtons). Several of these menus are dynamic - the items are added and removed programmatically, depending on actions the user has performed elsewhere (like the MRU files listing at the bottom of a File menu).

I我希望为用户提供一种从菜单中删除菜单项的简单方法。为此,我希望能够将ContextMenuStrip与其中一些菜单相关联,这些菜单将具有"删除"功能。或"删除",选择此选项后,将删除菜单项。

I'd like to offer a simple way for a user to remove a menu item from a menu. To do this, I would like to be able to associate a ContextMenuStrip to some of these menus, which would have a "Delete" or "Remove", that, when selected, would remove the menu item.

您可以在Windows"开始"菜单中看到类似的行为。如果您转到"开始"/"程序",则可以右键单击任何菜单项并获取上下文菜单以打开,删除或重命名所选菜单项。

You can see similar behavior in the Windows Start Menu. If you go to Start/Programs, you can right-click on any menu item and get a context menu to open, delete, or rename the selected menu item.

不幸的是,ToolStripDropDownButton不是派生自Windows.Forms.Control,因此它没有ContextMenuStrip属性。我尝试将ContextMenuStrip添加到父ToolStrip,但它只影响工具条及其顶级菜单项 - 右键单击​​子菜单不会调用上下文菜单。

Unfortunately, ToolStripDropDownButton is not derived from Windows.Forms.Control, so it does not have a ContextMenuStrip property. I attempted to add a ContextMenuStrip to the parent ToolStrip, but it only affects the tool strip and its top-level menu items - right-clicking on submenus does not invoke the context menu.

我还尝试在单个菜单项上覆盖MouseUp事件,处理右键单击并手动调出上下文菜单,但这会导致原始菜单关闭 - 这不是'对于想要仔细检查他/她是否正在删除正确菜单项的用户非常有帮助。重新打开原始菜单是一团糟。

I've also tried overriding the MouseUp event on individual menu items, handling the right-click and just bringing up the context menu manually, but this results in original menu closing - which isn't very helpful to a user who wants to double-check that he/she is removing the correct menu item. Reopening the original menu is a mess.

有没有人知道如何执行这个(看似简单的)任务?

Does anyone have an idea as to how to perform this (seemingly simple) task?

推荐答案

您可以通过两种不同的方法填充下拉列表 - 通过保护DropDownItems集合或将创建的ContextMenuStrip分配给ToolStripDropDownItem的DropDown属性。 ToolStripDropDownItem的另一个便利功能是DropDownItemClicked事件。这使您可以方便地在ToolStrip DropDown的click事件中同步每个项目。而且您也不必进入收藏品,我们只需将您点回的项目交还给您。以下示例显示了这些概念。

You can populate a dropdown via two different methods – either by hydrating the DropDownItems collection or assigning a created ContextMenuStrip to the ToolStripDropDownItem's DropDown property. Another handy feature of ToolStripDropDownItem is the DropDownItemClicked event. This allows you a handy way to not have to sync each item in a ToolStrip DropDown's click event. And you don't have to fish into the collection either, we just hand you back the item that was clicked on. The following sample shows these concepts.

// populate the DropDownItems Collection
ToolStripMenuItem veggiesMenuItem = new ToolStripMenuItem("Veggies");
veggiesMenuItem.DropDownItems.Add("Asparagus");
veggiesMenuItem.DropDownItems.Add("Bok Choy");
veggiesMenuItem.DropDownItems.Add("Cauliflower");
// Hook up the handler
veggiesMenuItem.DropDownItemClicked += new ToolStripItemClickedEventHandler(myDropDownItemClicked);

// assign a dropdown
ContextMenuStrip cms = new ContextMenuStrip();
cms.Items.Add("Apples");
cms.Items.Add("Bananas");
cms.Items.Add("Cherries");

ToolStripMenuItem fruitMenuItem = new ToolStripMenuItem("Fruit");
fruitMenuItem.DropDown = cms;
// Hook up the handler
fruitMenuItem.DropDownItemClicked += new ToolStripItemClickedEventHandler(myDropDownItemClicked);

// menustrip
MenuStrip ms = new MenuStrip();
ms.Items.Add(fruitMenuItem);
ms.Items.Add(veggiesMenuItem);
this.Controls.Add(ms);


这篇关于如何将ContextMenuStrip添加到ToolStripDropDownButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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