在MouseOver上更改TabControl的SelectedTab

时间:2012-03-12 05:18:07

标签: c# winforms mouse tabcontrol

我有一个带有TabControl的Windows窗体项目。

有人知道当你用指针悬停在SelectedTab上时如何更改{{1}}吗?

2 个答案:

答案 0 :(得分:6)

您可以使用TabControl的MouseMove事件来检测您的鼠标是否出现在任何标签上,然后可以选择它:

private void tabControl1_MouseMove(object sender, MouseEventArgs e)
{
    Rectangle mouseRect = new Rectangle(e.X, e.Y, 1, 1);
    for (int i = 0; i < tabControl1.TabCount; i++)
    {
        if (tabControl1.GetTabRect(i).IntersectsWith(mouseRect))
        {
            tabControl1.SelectedIndex = i;
            break;
        }
    }
}

答案 1 :(得分:1)

试试这个:

private void tabControl1_MouseMove(object sender, MouseEventArgs e)
    {
        for (int i = 0; i < tabControl1.TabCount - 1; i++)
        {
            if (tabControl1.GetTabRect(i).Contains(e.X, e.Y))
            {
                tabControl1.SelectedIndex = i;
            }
        }
    }