本文介绍了如何以一个项目的ContextMenuStrip响应点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有以下代码:

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?

推荐答案

这可以用下面的代码来完成:

This can be done using the following code:

ContextMenuStrip contexMenuuu = new ContextMenuStrip();

contexMenuuu.Items.Add("Edit ");
contexMenuuu.Items.Add("Delete ");
contexMenuuu.Show();
contexMenuuu.ItemClicked += new ToolStripItemClickedEventHandler(
    contexMenuuu_ItemClicked);

// ...

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



或者:

Or alternatively:

// ...

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

// ...

void item_Click(object sender, EventArgs e) {
    ToolStripItem clickedItem = sender as ToolStripItem;
    // your code here
}

这篇关于如何以一个项目的ContextMenuStrip响应点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:25