利用C#鼠标拖动TreeView节点

开发 后端
我们将介绍如何利用C#鼠标拖动TreeView节点,这些节点的访问,一般都需要进行遍历或其他操作。希望本文能对大家有所帮助。

我们将谈论C#鼠标拖动TreeView节点的问题,一般需要实现左键拖动,然后就是激发。C#鼠标拖动就是根据鼠标坐标确定要移动到的目标节点。

private void TVdepartment_ItemDrag(object sender, ItemDragEventArgs e)//左键拖动  
        {  
            if (e.Button == MouseButtons.Left)  
            {  
                DoDragDrop(e.Item, DragDropEffects.Move);   
            }  
        }  
 
        private void TVdepartment_DragEnter(object sender, DragEventArgs e)  
        {  
            if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))  
            {  
                e.Effect = DragDropEffects.Move;  
            }  
            else 
            {  
                e.Effect = DragDropEffects.None;  
            }  
        }  
 
    private void TVdepartment_DragDrop(object sender, DragEventArgs e)//拖动  
        {    //获得拖放中的节点  
            TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");  
            //根据鼠标坐标确定要移动到的目标节点  
            Point pt;  
            TreeNode targeNode;  
            pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));  
            targeNode = this.TVdepartment.GetNodeAt(pt);  
            //如果目标节点无子节点则添加为同级节点,反之添加到下级节点的未端  
            TreeNode NewMoveNode = (TreeNode)moveNode.Clone();  
            if (targeNode.Nodes.Count == 0)  
            {  
                targeNode.Parent.Nodes.Insert(targeNode.Index, NewMoveNode);  
            }  
            else 
            {  
                targeNode.Nodes.Insert(targeNode.Nodes.Count, NewMoveNode);  
            }  
            //更新当前拖动的节点选择  
            TVdepartment.SelectedNode = NewMoveNode;  
            //展开目标节点,便于显示拖放效果  
            targeNode.Expand();  
 
            //移除拖放的节点  
            moveNode.Remove();  
        } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.

利用C#鼠标拖动TreeView节点就介绍到这里。

【编辑推荐】

  1. C#委托实例简单分析
  2. 一个.NET委托的故事:彼得,老板和宇宙
  3. 解惑答疑:C#委托和事件
  4. 各版本.NET委托的写法回顾
  5. 换一个角度看.NET中的理解委托和事件
责任编辑:彭凡 来源: 博客园
相关推荐

2009-08-31 15:27:33

C# TreeView

2009-08-31 15:39:11

C#编写操作TreeV

2009-08-31 15:34:45

常用操作C# TreeView

2009-08-13 10:42:31

C#窗体拖动事件

2009-08-28 16:31:21

C# treeview

2009-08-19 16:50:32

Visual C#C#语言特性

2009-08-28 16:03:15

C#程序实现鼠标移动

2009-09-02 18:11:24

C#鼠标

2009-09-03 18:19:35

C#鼠标右键

2009-10-10 14:54:44

treeView1控件

2009-09-02 18:53:28

C#鼠标坐标

2009-08-18 11:17:37

C#添加鼠标右键

2009-09-03 16:50:35

C#鼠标形状

2009-09-01 10:35:59

C# WinForm控

2009-06-02 10:10:15

C#

2009-09-02 19:11:42

C#鼠标滚轮

2009-08-28 15:32:39

C#利用WMI获取数据

2009-09-02 18:34:28

C#鼠标事件

2009-09-07 18:08:25

C#鼠标指针

2009-08-28 15:52:23

C#利用sharpzi
点赞
收藏

51CTO技术栈公众号