Click here to Skip to main content
14,643,812 members
Rate this:
Please Sign up or sign in to 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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100