Hi all,
I'm trying to select a tab from the windows form's standard tab control on right clicking of mouse button on the tab button (when the context menu is shown)
The mouse event args gives me the location of the mouse pointer.
But the tab control doesn't expose any method or property to get the tab on which my mouse pointer is hovering.
If anyone has the solution I will be thankful if you can let me know.
Hoping to hear from you guys...
Thanks and Regards,

Windows Forms Tab Control problem !!
BCoelho
Mick,
An extraordinarily helpful link. Thanks for posting this!
While the solution you posted isn't exactly what I was looking for (I'm not looking for hover functionality), the information I needed was there in essence. For those who just want to achieve a simple "right click implies left click" functionality on their tabs, here is the code (quite similar to Mick's) that you need:
private void detailTabControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int clickedTabIndex = -1;
for (int index = 0; index < detailTabControl.TabCount; index++)
{
if (detailTabControl.GetTabRect(index).Contains(e.X, e.Y))
{
clickedTabIndex = index;
}
}
if (clickedTabIndex >= 0)
{
detailTabControl.SelectedIndex = clickedTabIndex;
}
}
}
Ling A
Hi,
If u are able to get the tab control from the context menu click point then u can easily get the tabpage from the tabcontrol.
The tab control contains a property SelectedTab which gives the tabpage on which the click was done.
Hope this was what u wanted else u can post the code so that we can know the requirement better.
Thanks
Subasree
eburke
For anyone who needs a ContextMenu on Tab header only, add to this
if (clickedTabIndex >= 0)
{
detailTabControl.SelectedIndex = clickedTabIndex;
menu.Show(detailTabControl, e.Location);
}
where the menu is a ContextMenu you created.
vasu_ve
Hi,
The SelectedTab property of TabControl gives the currently selected Tab but the problem is, a tab is selected only when a left click of the mouse is done on the tab. On right click no tab is selected. Thus, if you check SelectedTab property, it will contain the tabpage which is currently active; not the tabpage which is clicked.
For example, Consider that you have 2 tabpages in the control, say 0 and 1. Tab 0 is currently selected. I bring the mouse over tab 1 and do a right click. In this case SelectedTab will give me 0 where as I want 1 to get selected.
Hope my example explains my problem. Sorry, I don't have any code to demonstrate the problem.
Thanks and regards,
Aryadip.
Tommy Vinson
Aryadip,
I am having the same problem. Did you find a solution which works If so, care to offer a code snippet or two
Adraw
David F.
You'll find an example on my site titled "Associate a ContextMenu with the TabItem headers of a Tabcontrol." You should be able to extract the method from that.
http://www.dotnetrix.co.uk/tabcontrols.html
stacez
Thanks for the code, I was searching for this!!
Here's my version of the code:
for (int index = 0; index < detailTabControl.TabCount; index++){
if (detailTabControl.GetTabRect(index).Contains(e.X, e.Y))
{
detailTabControl.SelectedIndex = index;
break;
}
}