简体   繁体   中英

How to get Context Menu click event for a sub menu item

I have the following code, which creates a context menu, with a sub menu;

ContextMenuStrip oContextMenuStrip = new ContextMenuStrip();
ToolStripSeparator oToolStripSeparator = new ToolStripSeparator();
ToolStripMenuItem oToolStripItem = new ToolStripMenuItem();

oToolStripItem.Text = "Change Status";

oToolStripItem.DropDownItems.Add("Booked", 1);
oToolStripItem.DropDownItems.Add("Pending", 2);
oToolStripItem.DropDownItems.Add("Cancelled", 3);

oContextMenuStrip.Items.Add(oToolStripItem);
oContextMenuStrip.Items.Add(oToolStripSeparator);

oContextMenuStrip.ItemClicked += new ToolStripItemClickedEventHandler(ContextMenuClick_ItemClicked);

void ContextMenuClick_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    ToolStripItem oToolStripItem = e.ClickedItem;

}

The problem I have is the event ContextMenuClick_ItemClicked is only called when the user clicks on the Change Status menu. What event do I need to that I can capture the click event of the item like 'Booked'.

Use another version of the ToolStripItemCollection.Add Method:

public ToolStripItem Add(
    string text,
    Image image,
    EventHandler onClick
)

ToolStripItemCollection.Add Method (String, Image, EventHandler)

For example:

oToolStripItem.DropDownItems.Add("Booked", null, (s, e) => BookedClicked());

And then:

private void BookedClicked() {
  // Do your magic
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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