Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to respond to a ContextMenuStrip item click

Currently I have the following code:

ContextMenuStrip contexMenuuu = new ContextMenuStrip();

contexMenuuu.Items.Add("Edit ");
contexMenuuu.Items.Add("Delete " );
contexMenuuu.Show();

How can I add an event to be processed when an item gets clicked?

like image 503
user725177 Avatar asked Apr 26 '11 10:04

user725177


2 Answers

This can be done using the following code:

ContextMenuStrip contexMenu = new ContextMenuStrip();

contexMenu.Items.Add("Edit ");
contexMenu.Items.Add("Delete ");
contexMenu.Show();
contexMenu.ItemClicked += new ToolStripItemClickedEventHandler(
    contexMenu_ItemClicked);

// ...

void contexMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
    ToolStripItem item = e.ClickedItem;
    // your code here
}

Or alternatively:

// ...

ToolStripItem item = contexMenu.Items.Add("Edit ");
item.Click += new EventHandler(item_Click);

// ...

void item_Click(object sender, EventArgs e) {
    ToolStripItem clickedItem = sender as ToolStripItem;
    // your code here
}
like image 192
DevExpress Team Avatar answered Nov 13 '22 13:11

DevExpress Team


Add method returns ToolStripItem. So you can add handle to Click event

        var item = contexMenuuu.Items.Add("Edit ");
        item.Click += methodToBeInvoked;
like image 28
Stecya Avatar answered Nov 13 '22 14:11

Stecya