如何将clickhandler添加到以编程方式创建的menustrip中

问题描述:

我有一个动态的contextmenustrip,我在运行时使用此代码将项目放入其中



Hi, i have a dynamic contextmenustrip that i put items to it in runtime using this code

strp[i] = new ContextMenuStrip();
strp[i].Items.Add("Delete from this List", null,showMessageBox);





i想要当项目被点击时显示一个消息框,



i不能使用方法。



i want to show a messagebox when the item clicked,

i can't use a method lik

void disp(object sender, EventArgs e)
{
messagebox.show("hi");
}





因为点击处理程序必须位于创建项目的确切行中,因为我正在创建一个循环contextmenustrip具有不同的名称和相同的文本,如果我这样做每个contextmenustrip项目将范围相同的点击处理程序,所以它必须在创建项目的确切行,我该怎么做?



我尝试了什么:



i试过这段代码



because the click handler must be in the exact line where the item is created because i'm making a loop of contextmenustrip with different names and same text, if i do that every contextmenustrip items will scope to the same click handler, so it must be in the exact line where the item is being created, how i can do that?

What I have tried:

i have tried this code

strp[i].Items.Add("Delete from this List", null, Click +=(b,g) => { MessageBox.Show("hi") });





但是它给出了错误(不能将void更改为system.handler)我试图将其更改为



but it gives error (cannot change void to system.handler) i tried to change it to

object sender, eventhandler t



但它不起作用,我尝试了很多其他代码都没有工作


but it doesn't work, i tried alot of other codes none of them worked

试试这个:

Try this:
ContextMenuStrip cms = new ContextMenuStrip();
myTextBox.ContextMenuStrip = cms;
cms.Items.Add("Test", null, (s, a) => { MessageBox.Show("Hi!"); });