• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

.netC#Treeview的Node拖动,Node节点上下移动

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

定义一个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();                                                                                                                                                                    
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#去掉字符串中特定ASC码字符发布时间:2022-07-10
下一篇:
C#网址发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap