zoukankan      html  css  js  c++  java
  • .net C# Treeview的Node拖动,Node节点上下移动

    定义一个TreeView 名字为TVdepartment,设置allowdrop为True

    //移动代码                                                                           
                                                                                         
    private void MiMoveUp_Click(object sender, EventArgs e) //向上移动                   
    {                                                                                      
        TreeNode Node = TVdepartment.SelectedNode;                                       
        TreeNode PrevNode = Node.PrevNode;                                               
        if (PrevNode != null)                                                            
        {                                                                                
                                                                                         
            TreeNode NewNode = (TreeNode)Node.Clone();                                   
            if (Node.Parent == null)                                                     
            {                                                                            
                TVdepartment.Nodes.Insert(PrevNode.Index, NewNode);                      
            }                                                                            
            else                                                                         
            {                                                                            
                Node.Parent.Nodes.Insert(PrevNode.Index, NewNode);                       
            }                                                                            
            Node.Remove();                                                               
            TVdepartment.SelectedNode = NewNode;                                         
                                                                                         
                                                                                         
        }                                                                                
                                                                                         
                                                                                         
    }                                                                                    
    private void MiMoveDown_Click(object sender, EventArgs e)//向下移动                  
    {                                                                                                    
        TreeNode Node = TVdepartment.SelectedNode;                                       
        TreeNode NextNode = Node.NextNode;                                               
        if (NextNode != null)                                                            
        {                                                                                
                                                                                         
            TreeNode NewNode = (TreeNode)Node.Clone();                                   
            if (Node.Parent == null)                                                     
            {                                                                            
                TVdepartment.Nodes.Insert(NextNode.Index + 1, NewNode);                  
            }                                                                            
            else                                                                         
            {                                                                            
                Node.Parent.Nodes.Insert(NextNode.Index + 1, NewNode);                   
            }                                                                            
            Node.Remove();                                                               
            TVdepartment.SelectedNode = NewNode;                                         
                                                                                         
                                                                                         
        }                                                                                
    }                                                                                    
    //拖动代码                                                                           
    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();                                                                                                                                                                    
    }

  • 相关阅读:
    IELTS Writing Task 2: 'music' essay
    leetcode 368. 最大整除子集
    820复试算法 快排找第 k 小
    ASP.NET后台生成随机验证码
    Oracle身份证验证方法
    ASP.NET 存储过程导入(oracle)返回导入成功数和导入失败数
    C#执行参数为游标 返回一个记录集的Oracle存储过程
    C#执行带参数的Oracle存储过程
    往ORACLE数据库中插入XML数据
    存储过程 将数据插入到临时表,再根据条件判断 插入到不同的表
  • 原文地址:https://www.cnblogs.com/zhwl/p/2815021.html
Copyright © 2011-2022 走看看