Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out what tab I'm right clicking on in winforms tabcontrol?

Tags:

c#

tabcontrol

I'm making a context menu strip appear during the right clicking of either a selected or unselected tab in the tab strip of a winforms tabcontrol. It's going to have close, and close all but this for now. Anyway, I need to be able to capture which tab the mouse is over when the right click is pressed. Anyone know how?

Another solution that I'd accept is one that selects the unselected tab with a right click before the context menu is shown.

like image 766
Isaac Bolinger Avatar asked Sep 19 '10 23:09

Isaac Bolinger


2 Answers

In your mouse click event you can add this code to find it, if tabs is your tabcontrol

for (int i = 0; i < tabs.TabCount; ++i) {
     if (tabs.GetTabRect(i).Contains(e.Location)) {
         //tabs.Controls[i]; // this is your tab
     }
}
like image 182
Patrick Avatar answered Sep 26 '22 00:09

Patrick


This could help, it captures the position of your right click with the mouse and if it's on the rectangle of any tab, that tab will be selected and get's right menu to be showed

private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
      if (e.Button == MouseButtons.Right)
    {
            for (int i = 0; i < tabs.TabCount; ++i)

                {

                if (tabs.GetTabRect(i).Contains(e.Location)) 
                     {

                tabControl1.SelectTab(i);

                this.contextMenuStrip1.Show(this.tabControl1, e.Location);

                     }

                }
    }
}

Have fun :)

like image 31
Ahmed Nefzaoui Avatar answered Sep 25 '22 00:09

Ahmed Nefzaoui