简体   繁体   English

如何使用父控件的上下文菜单覆盖文本框上下文菜单?

[英]How to override textbox context menu with parent control's context menu?

I have a Panel with various child controls on it. 我有一个带有各种子控件的面板。 I assigned a context menu to the Panel and it comes up whether I right click on the Panel or on the controls... except for the TextBox control because it comes with its own built in context menu. 我为面板分配了一个上下文菜单,无论是在面板上还是在控件上单击鼠标右键都将显示它……除了TextBox控件,因为它带有自己的内置上下文菜单。

I would rather not have to set the context menu on controls directly because they are dynamically generated, and they are generated in a dll outside of the GUI that I'm using them on. 我宁愿不必直接在控件上设置上下文菜单,因为它们是动态生成的,并且它们是在我正在使用它们的GUI之外的dll中生成的。 Is there any way I can force the Panel's context menu to come up instead of the TextBox's context menu without having to set the ContextMenuStrip property directly on the TextBox? 有什么方法可以强制面板的上下文菜单而不是TextBox的上下文菜单出现,而不必直接在TextBox上设置ContextMenuStrip属性?

There isn't a work-around for without having to set the ContextMenuStrip property directly on the TextBox : without having to set the ContextMenuStrip property directly on the TextBox

Simply assign it. 只需分配它。

textBox1.ContextMenuStrip = panel1.ContextMenuStrip;

or 要么

textBox1.ContextMenuStrip = textBox1.Parent.ContextMenuStrip;

You can alternatively loop through the child controls of the panel and assign the menu strip to each child control. 您也可以循环浏览面板的子控件,并将菜单条分配给每个子控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM