简体   繁体   English

C#TabControl ContextMenuStrip

[英]C# TabControl ContextMenuStrip

In a user control of mine I implement a tab control that should programmatically manage tab pages. 在我的用户控件中,我实现了一个选项卡控件,该控件应以编程方式管理选项卡页。 I connected the tab control with a context menu strip with the menu items "Add", "Edit", "Delete" to respectively add a new tab page, edit or delete an existing one. 我将选项卡控件与带有菜单项“添加”,“编辑”,“删除”的上下文菜单条相连,以分别添加新的选项卡页,编辑或删除现有的选项卡页。 Initially, the tab control does not have any tab pages, and in this case the context menu strip does not appear on right mouse click; 最初,选项卡控件没有任何选项卡页,在这种情况下,右键单击菜单不会显示上下文菜单栏; if a tab page is there, the context menu strip works as required. 如果有选项卡页,则上下文菜单栏将按要求工作。 At that, the context menu strip is attached to the tab control itself, not to any of the tab pages. 那时,上下文菜单条附加到选项卡控件本身,而不附加到任何选项卡页。 I find this state quite illogical, and my question is whether there is any possibility to make the context menu work attached to a tab control work even if the tab control is empty? 我发现这种状态非常不合逻辑,我的问题是,即使选项卡控件为空,是否有可能使上下文菜单工作附加到选项卡控件上?

Empty TabControl does not receive mouse events. 空的TabControl不接收鼠标事件。 They are passed to the underlying control. 它们被传递给基础控件。

You can do the following. 您可以执行以下操作。

Put the TabControl inside a Panel of the same size. TabControl放在相同大小的Panel中。 Assign the same context menu to this Panel . 将相同的上下文菜单分配给此Panel Then, when TabControl is empty, mouse events will be passed to the Panel and menu will be shown too. 然后,当TabControl为空时,鼠标事件将传递给Panel ,菜单也将显示。

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

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