Reputation:
I'm trying to implement my tab control in such a way that a user doesn't need to click on the the tab page head before the tab page is selected. What i want to do is select the tab page as soon as the user hovers the tab page header.
I'm currently using this mousemove event
foreach (TabPage page in tabControl1.TabPages)
{
if (e.Location.Y == page.Bounds.Top - 15)
{
tabControl1.SelectedTab = page;
}
}
I get a weird behaviour when I hover the tab page header at the selected location. All the tab pages are getting selected one by one. (i.e It keeps selecting all the tab pages not the one that is hovered)
What am i doing wrong and what can i do to achieve what i want?
Upvotes: 0
Views: 1825
Reputation: 2904
You would need to check the e.Location.X
as well. Now you're just looking for the y value, and since each tab page head are on the same "height" the if statement will be true for all tab pages.
EDIT: Use the function GetTabRect(i)
instead:
for (int tab = 0; tab < tabControl1.TabCount; tab++) {
if (tabControl1.GetTabRect(tab).Contains(e.Location)) {
tabControl1.SelectedIndex = tab;
break;
}
}
Upvotes: 3