如何使用单个上下文菜单栏 [英] How to use single Context Menu Strip

查看:130
本文介绍了如何使用单个上下文菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想将一个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 ?

推荐答案

此讨论 [ ^ ]可以为您提供帮助.
This discussion[^] could help you.


与所有其他控制事件一样,第一个参数是发送者".
如果将其强制转换为ContextMenuStrip控件,则SourceControl属性是右键单击以提供菜单框的控件:
As with all other control events, the first parameter is "sender".
If you cast this to a ContextMenuStrip control, then the SourceControl property is the control that was right clicked to give you the menu box:
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!


这篇关于如何使用单个上下文菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆