ContextMenuStrip动态分配给下拉按钮项C#WINFORM

时间:2012-10-10 23:25:18

标签: c# winforms user-interface contextmenu strip

我正在WINFORMS中构建一个Web浏览器。我在工具条中有一个dropdown按钮。 dropdown的项目是在运行时从XML文件动态添加的。下拉按钮用于查看博彩公司。我有contextmenu添加和删除书签选项。我看到contextmenuproperty没有dropdownbutton items.我需要在下拉按钮中为每个项目右键单击显示上下文菜单。我甚至找不到项目的mouseclick事件。因此,没有e.location

//I have got these events do far (just handles the click)
    private void bookmarksDropDownButton2_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
                tabControlBrowser.SelectedTab = tabControlBrowser.TabPages[tabControlBrowser.TabPages.Count - 1];
                getCurrentWebBrowser().Navigate(dictionary[e.ClickedItem.Text]);
                AddressComboBox1.Text = dictionary[e.ClickedItem.Text];
            }

我在我的代码中以这种方式连接事件

    foreach (ToolStripItem item in bookmarksDropDownButton2.DropDownItems)
            {
                item.MouseDown += new MouseEventHandler(item_MouseDown);
            }
    private void item_MouseDown(object sender, MouseEventArgs e)
        {
              BOOKcontextMenuStrip1.Show(e.Location);
              //this one pop's out the menu at the uppermost corner of the window.
        }

enter image description here

1 个答案:

答案 0 :(得分:1)

遗憾的是,您无法将ContextMenuToolStrip中的各个项目相关联。您需要做的是在右键单击DropDownButton时,在其MouseDown事件(检查哪个鼠标按钮)到工具条本身时设置上下文菜单。

  • 或者在这种情况下:

您需要为每个项目动态添加MouseDown事件的处理程序,以便您可以选择实际项目(悬停)。右键单击项目时,将上下文菜单分配给工具条本身(现在可以存储在f.ex.上下文菜单的Tag属性中单击的项目的ID)。

在ContextMenu的Closed事件中,您可以从工具条(.ContextMenuStrip=null)中删除该菜单。

对不起,我现在没有C#示例。让我知道它是否足够清楚,在这种情况下我会尝试添加一个例子。

相关问题