Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

.net - C# Switch tabs(tabcontrol) while dragging and hovering over a tab

If someone can help me with this it will be much appreciated, all I want is code that will allow me to change tab pages while dragging a treenode from a treeview that is OUTSIDE the tabcontrol AND hovering over a tabpage that is NOT already open(selected).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...