[Solved] C# Switch tabs(tabcontrol) while dragging and hovering over a tab


The DragOver event will be fired when the mouse moves over the tabcontrol while the drag action is still in effect. You can use similar logic to the mousemove logic in Change SelectedTab of TabControl on MouseOver in your DragOver handler to make the tabs switch.

Edit:

I did a little MSDN research and found a likely issue. DragOver coordinates are ScreenCoordinates while the tab rectangle in the sample code is in client coordinates. You will need to convert the drag coordinates before the hit check.

            Point clientPoint = tabControl1.PointToClient(new Point(e.X, e.Y));

Edit2:

Put together a trivial app with a TreeView and a TabControl and the following DragOver handler switched tabs correctly as I dragged over the tabs:

    private void tabControl1_DragOver(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.All;

        Point clientPoint = tabControl1.PointToClient(new Point(e.X, e.Y));

        for (int i = 0; i < tabControl1.TabCount; i++)
        {
            if (tabControl1.GetTabRect(i).Contains(clientPoint) && tabControl1.SelectedIndex != i)
            {
                tabControl1.SelectedIndex = i;
            }
        }

    }

2

solved C# Switch tabs(tabcontrol) while dragging and hovering over a tab