Change SelectedTab of TabControl on MouseOver

*爱你&永不变心* 提交于 2019-11-27 07:22:54

问题


I have a Windows Forms project with a TabControl.

Does anyone know how to change the SelectedTab when you hover over it with the pointer?


回答1:


You can use TabControl's MouseMove event to detect whether your mouse is present on any tab and then can select it:

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;
        }
    }
}



回答2:


Try this:

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;
            }
        }
    }


来源:https://stackoverflow.com/questions/9662083/change-selectedtab-of-tabcontrol-on-mouseover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!