加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用拖放方式时,是否可以使TreeView展开用户悬停的节点?

发布时间:2020-12-15 06:33:58 所属栏目:百科 来源:网络整理
导读:简单来说: 当拖放操作正在进行中时,.Net 2.0中是否有任何内置函数在悬停时展开TreeNode? 我在Visual Studio 2005中使用C#. 更详细地说: 我已经使用多层次的多重树(考虑组织图或文件/文件夹对话框)填充了Treeview控件,我想使用拖放来移动树中的节点. 拖放
简单来说:

当拖放操作正在进行中时,.Net 2.0中是否有任何内置函数在悬停时展开TreeNode?

我在Visual Studio 2005中使用C#.

更详细地说:

我已经使用多层次的多重树(考虑组织图或文件/文件夹对话框)填充了Treeview控件,我想使用拖放来移动树中的节点.

拖放代码工作正常,我可以放到任何可见的节点上,但是我希望我的控件像Windows Explorer一样在文件夹窗格中拖动文件时的行为.具体来说,我希望每个文件夹打开,如果悬停在1/2秒左右.

我已经开始使用线程和睡眠方法开发解决方案,但是我遇到了问题,并且想知道是否已经有了某些功能,如果不是我会指点并学习如何使用线程(这是关于时间,但我是希望快速得到这个应用程序)

在拖放模式下悬停时,需要编写自己的代码来处理扩展树节点吗?

解决方法

您可以使用DragOver事件;当您拖动对象时,它会反复触发
延迟后打开可以非常容易地使用两个额外的变量来记录鼠标下的最后一个对象和时间.不需要线程或其他技巧(在我的例子中,lastDragDestination和lastDragDestinationTime)

从我自己的代码:

TreeNode lastDragDestination = null;
DateTime lastDragDestinationTime;

private void tvManager_DragOver(object sender,DragEventArgs e)
{
    IconObject dragDropObject = null;
    TreeNode dragDropNode = null;

    //always disallow by default
    e.Effect = DragDropEffects.None;

    //make sure we have data to transfer
    if (e.Data.GetDataPresent(typeof(TreeNode)))
    {
        dragDropNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
        dragDropObject = (IconObject)dragDropNode.Tag;
    }
    else if (e.Data.GetDataPresent(typeof(ListViewItem)))
    {
        ListViewItem temp (ListViewItem)e.Data.GetData(typeof(ListViewItem));
        dragDropObject = (IconObject)temp.Tag;
    }

    if (dragDropObject != null)
    {
        TreeNode destinationNode = null;
        //get current location
        Point pt = new Point(e.X,e.Y);
        pt = tvManager.PointToClient(pt);
        destinationNode = tvManager.GetNodeAt(pt);
        if (destinationNode == null)
        {
            return;
        }

        //if we are on a new object,reset our timer
        //otherwise check to see if enough time has passed and expand the destination node
        if (destinationNode != lastDragDestination)
        {
            lastDragDestination = destinationNode;
            lastDragDestinationTime = DateTime.Now;
        }
        else
        {
            TimeSpan hoverTime = DateTime.Now.Subtract(lastDragDestinationTime);
            if (hoverTime.TotalSeconds > 2)
            {
                destinationNode.Expand();
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读