C# 拖动和悬停在选项卡上时切换选项卡(tabcontrol) [英] C# Switch tabs(tabcontrol) while dragging and hovering over a tab

查看:49
本文介绍了C# 拖动和悬停在选项卡上时切换选项卡(tabcontrol)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以帮助我,我将不胜感激,我想要的只是允许我更改标签页的代码,同时从标签控件外部的树视图中拖动树节点并将鼠标悬停在尚未存在的标签页上打开(选中).

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).

推荐答案

当鼠标在 tabcontrol 上移动而拖动动作仍然有效时,会触发 DragOver 事件.您可以使用与 在 MouseOver 上更改 TabControl 的 SelectedTab 中的 mousemove 逻辑类似的逻辑 在您的 DragOver 处理程序中切换标签.

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.

我做了一些 MSDN 研究,发现了一个可能的问题.DragOver 坐标是屏幕坐标,而示例代码中的选项卡矩形是客户端坐标.您需要在命中检查之前转换拖动坐标.

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

编辑 2:

将一个简单的应用程序与 TreeView 和 TabControl 放在一起,以下 DragOver 处理程序在我拖动选项卡时正确切换选项卡:

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

    }

这篇关于C# 拖动和悬停在选项卡上时切换选项卡(tabcontrol)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆