Search code examples
c#tabcontrolcontextmenustrip

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?


Solution

  • Empty TabControl does not receive mouse events. They are passed to the underlying control.

    You can do the following.

    Put the TabControl inside a Panel of the same size. Assign the same context menu to this Panel. Then, when TabControl is empty, mouse events will be passed to the Panel and menu will be shown too.