Change SelectedTab of TabControl on MouseOver

前端 未结 2 596
青春惊慌失措
青春惊慌失措 2020-12-11 07:30

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?

相关标签:
2条回答
  • 2020-12-11 07:55

    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;
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-11 08:16

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题