1

I am writing a C# WinForm application. I have a Tab Control and I've already preset all functions such as adding new tabs by menu, and double clicking tabs to delete. My issue now is I want to create a tab every time I double click the empty grey space of the tab control.

I've been searching for about an hour now and a lot of what I find is dealing with WPF and I'd rather not delve into those anytime soon. So I'm looking for a matter to fit my means of detecting the double click. I've tried double click and mouse double click double click works on tabs, and mouse double click does the same except it captures the mouse event arguments. So then I'm able to use

e.Location

Except it only provides that information on the tabs themselves. Anyone have any suggestions to capture that double click outside the tabs themselves? I've tried go up in parents such as mouse double click on the window itself, tried creating a completely opaque element that may handle the clicks instead but I've hit a brick wall every time.

 private void Tabs_MouseDoubleClick(object sender, MouseEventArgs e)
 {
      //only works when double clicking tabs
      MessageBox.Show(e.Location.ToString());
 }
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • I don't know how to do that but I do know that both doubleclick actions seem rather non-intuitive to me. Adding an X to the tabs and a +-page to the tab control is the standard users will expect. - Lol: Just noted that the exact same reaction [here](http://stackoverflow.com/questions/4983389/how-to-handle-a-double-click-on-a-tabcontrols-tabs-panels-empty-space?rq=1) – TaW Sep 04 '16 at 21:35
  • 1
    Take a look at ['TabControl with Close and Add Button'](http://stackoverflow.com/questions/36895089/tabcontrol-with-close-and-add-button) or ['WinForms TabControl - Add New Tab Button (+)'](http://stackoverflow.com/questions/6738126/winforms-tabcontrol-add-new-tab-button) – Reza Aghaei Sep 04 '16 at 22:07
  • 2
    It seems that part is transparent to mouse and all mouse events pass through the part and will be received by the control which is behind that part. So as a workaround you can put the `TabControl` in a `Panel` and set the `Dock` property of `TabControl` to true, and the handle `DoubleClick` event of `Panel`. – Reza Aghaei Sep 04 '16 at 23:30
  • TaW I don't think that's an industry standard by far to be honest. Some applications may do this but I know for a fact that NP++ has a double click action and am going to be trying Reza Aghaei method – EasyBB Sep 05 '16 at 00:03
  • 'NP++' being what? Double-clicking is always understood to activate something, not delete it. Well, unless you're on a Mac ;-) – TaW Sep 05 '16 at 10:40
  • 1
    @Reza: Your suggested workaround works fine and as it adresses the issue instead of pointing out just (real or percieved) flaws in the op design it ought to be an answer, imo. – TaW Sep 05 '16 at 10:45
  • @TaW I was going to post an answer, but before posting an answer, after some search to verify my answer, I found the linked post and decided to mark the post as duplicate. – Reza Aghaei Sep 05 '16 at 12:18
  • Should it be posted an answer? – Reza Aghaei Sep 05 '16 at 12:19
  • Yes, I think so. I was stumped to find that I just couldn't catch the click; even tried a subclass but didn't think of them getting passed through. I didn't want to throw a global mouse hook at it and why would one, when your solution is so simple. So let it not hide in a comment, where folks will not find it.. – TaW Sep 05 '16 at 12:22
  • @TaW NP++ is Notepad++ if you double click the empty space (header) it creates a new tab, and if you double click the tab it'll delete it. But I've accepted Reza's answer as it's the best solution out there. – EasyBB Sep 06 '16 at 20:55
  • _if you double click the empty space (header) it creates a new tab_ true and fine with me _and if you double click the tab it'll delete it_ no it doesn't, by default, but one can configure it to do so. I would normally not recommend. – TaW Sep 06 '16 at 21:08
  • I've like this style of tabs since I've seen it on NP++ doesn't take away from the application it only adds to the accessibility. – EasyBB Sep 06 '16 at 21:40

1 Answers1

4

It seems that part of TabControl is transparent to mouse and all mouse events pass through the part and will be received by the control which is behind the part.

So as a workaround you can put the TabControl in a Panel and set the Dock property of TabControl to Fill, and the handle DoubleClick event of Panel. You also have the option of mouse hooks, but I believe the panel option is simpler.

Mouse-trasparent part of control is shown in red color in below image. Also borders of control are part of that area.

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398