问题描述
我想将一个ContextMenuStrip用于不同的编辑框(或文本标签). ContextMenuStrip仅包含编辑"和删除"条目,我想从我的应用程序中编辑或删除相应的数据.我已经编写了相关代码,但是如何找到从哪个文本框(或文本标签)中单击上下文菜单?有人可以帮我吗?
Hi,
I want to use a single ContextMenuStrip for different edit boxes (or text labels). The ContextMenuStrip is simply containing Edit and Remove entries and I want to Edit or Remove the corresponding data from my application. I have written the relevant code but how could I found that the context menu is clicked from which text box (or text label)? Can any one help me ?
推荐答案
private void contextMenuStrip1_Click(object sender, EventArgs e)
{
ContextMenuStrip cms = sender as ContextMenuStrip;
if (cms != null)
{
Console.WriteLine(cms.SourceControl.Name);
}
}
请注意,SourceControl是您与菜单关联的最高级别:如果仅将ContextMenuStrip与窗体关联,则它将始终作为SourceControl的窗体.您必须分别将ContextMenuStrip指定为每个控件的Control.ContextMenuStrip属性!
Do note that the SourceControl is the highest level you have associated the menu with: if you have associated the ContextMenuStrip with the form only, then it will always be the form as the SourceControl. You have to specify the ContextMenuStrip as the Control.ContextMenuStrip property of each control individually!
这篇关于如何使用单个上下文菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!