Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 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");
}


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") });


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
Posted
Updated 15-Jul-18 4:23am

1 solution

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900