目前,我有以下代码:
ContextMenuStrip contexMenuuu = new ContextMenuStrip();
contexMenuuu.Items.Add("Edit ");
contexMenuuu.Items.Add("Delete " );
contexMenuuu.Show();
单击某项时如何添加要处理的事件?
最佳答案
可以使用以下代码完成此操作:
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
}
或者:
// ...
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
}
关于c# - 如何响应ContextMenuStrip项目单击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5789023/