简体   繁体   English

捕获单击事件以编程方式生成的上下文菜单子菜单

[英]Trapping a click event for a programmatically generated context menu sub-menu

I am trying to catch a click event on a context menu submenu created dynamically by the following code. 我试图捕获由以下代码动态创建的上下文菜单子菜单上的click事件。 The context menu cmList is created in the designer, and the click event code is added from the properties menu. 上下文菜单cmList在设计器中创建,并且单击事件代码从属性菜单中添加。

for (int i = 0; i <= sTagsContext.GetUpperBound(0); i++)
{
    cmListTags.Items.Add(sTagsContext[i]);
    ToolStripMenuItem submenu = new ToolStripMenuItem();                  
    submenu.Text = i.ToString();
    submenu.Image = Properties.Resources.InfoBig;

    (cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(submenu);                    
     chkListTags.ContextMenuStrip = cmListTags;
}

How can I create code to be executed when the submenu of any of the context menu items is clicked and have the identity of the submenu item (set in the text property) available? 当单击任何上下文菜单项的子菜单并使该子菜单项的标识(在text属性中设置)可用时,如何创建要执行的代码?

I have tried adding an event handler using 我尝试使用添加事件处理程序

(cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(i.ToString(), Properties.Resources.InfoBig, new EventHandler(InfoClicked));

where I create the function 我在哪里创建函数

public void InfoClicked(object sender, EventArgs e)
{
}

This function is called when the sub-menu is clicked but neither sender nor e has any information about the sub-menu item clicked - sender is null and e is empty. 单击子菜单但发件人或e都没有有关子菜单项被单击的任何信息时,将调用此函数-发件人为null,e为空。

If I set e to be type ToolStripItemClickedEventArgs and change the Dropdown addition line to 如果我将e设置为ToolStripItemClickedEventArgs并将Dropdown加法行更改为

(cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(i.ToString(), Properties.Resources.InfoBig, new ToolStripItemClickedEventHandler(InfoClicked));

I get a compile time type mismatch for the last parameter of DropDownItems.Add. 我得到了DropDownItems.Add的最后一个参数的编译时类型不匹配。

You can use an anonymous method - a method body without a name. 您可以使用匿名方法 -没有名称的方法主体。

int index = i;
cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(
     i.ToString(), 
     Properties.Resources.InfoBig, 
     (s, args) => {
         MessageBox.Show(index.ToString(); 
} ));

Since the anonymous method is declared in place , it has access to the local variable i . 由于匿名方法已在适当位置声明,因此可以访问局部变量i So in this way you don't need to use sender . 因此,您无需使用sender

Edit : Turns out i is being modified in the for loop. 编辑 :原来正在for循环中被修改。 So I have to use a local copy index to keep its value. 因此,我必须使用本地副本索引来保留其值。

And as to your 2nd question, 关于第二个问题,

I get a compile time type mismatch for the last parameter of DropDownItems.Add. 我得到了DropDownItems.Add的最后一个参数的编译时类型不匹配。

This is because the signature of InfoClicked does not match the signature of delegate ToolStripItemClickedEventHandler . 这是因为InfoClicked的签名与委托ToolStripItemClickedEventHandler的签名不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM