我有以下代码,它创建一个上下文菜单,带有子菜单;
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;
}
我遇到的问题是事件ContextMenuClick_ItemClicked仅在用户单击“更改状态”菜单时调用。我需要什么样的事件才能捕获项目的点击事件,例如'Booked'。
答案 0 :(得分:2)
使用另一版本的ToolStripItemCollection.Add方法:
public ToolStripItem Add(
string text,
Image image,
EventHandler onClick
)
ToolStripItemCollection.Add Method (String, Image, EventHandler)
例如:
oToolStripItem.DropDownItems.Add("Booked", null, (s, e) => BookedClicked());
然后:
private void BookedClicked() {
// Do your magic
}