How can I move nodes between two treeview controls

Abdelmalek Aitouche 176 Reputation points
2021-02-23T13:17:21.767+00:00

I am trying to move nodes between two treeview controls without success. How can I drag/drop one node from treeview1 to treeview2. I managed to do it inside the same treeview but I failed when I tried between two treeview controls. Can you please help. Great thanks.
@Castorix31

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,579 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-02-24T07:02:56.94+00:00

    Hi @Abdelmalek Aitouche ,

    I am trying to move nodes between two treeview controls

    You can refer to the following example to move nodes between two treeview controls.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            Dim node As TreeNode = treeView1.Nodes.Add("A")  
            node.Nodes.Add("A1")  
            node.Nodes.Add("A2")  
            Dim node2 As TreeNode = TreeView1.Nodes.Add("C")  
            node2.Nodes.Add("C1")  
            node2.Nodes.Add("C2")  
      
            node = treeView2.Nodes.Add("B")  
            node.Nodes.Add("B1")  
            node.Nodes.Add("B2")  
            TreeView2.AllowDrop = True  
            TreeView1.AllowDrop = True  
      
            AddHandler Me.TreeView1.DragDrop, AddressOf Me.Tree_DragDrop  
            AddHandler Me.TreeView1.DragOver, AddressOf Me.Tree_DragOver  
            AddHandler Me.TreeView1.MouseDown, AddressOf Me.Tree_MouseDown  
            AddHandler Me.TreeView2.DragDrop, AddressOf Me.Tree_DragDrop  
            AddHandler Me.TreeView2.DragOver, AddressOf Me.Tree_DragOver  
            AddHandler Me.TreeView2.MouseDown, AddressOf Me.Tree_MouseDown  
        End Sub  
        Private Sub Tree_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)  
            Dim tree As TreeView = CType(sender, TreeView)  
            Dim node As TreeNode = tree.GetNodeAt(e.X, e.Y)  
            tree.SelectedNode = node  
      
            If node IsNot Nothing Then  
                tree.DoDragDrop(node, DragDropEffects.Copy)  
            End If  
        End Sub  
      
        Private Sub Tree_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)  
            Dim tree As TreeView = CType(sender, TreeView)  
            e.Effect = DragDropEffects.None  
            Dim nodeSource As TreeNode = CType(e.Data.GetData(GetType(TreeNode)), TreeNode  
    
            If nodeSource IsNot Nothing Then      
                If Not nodeSource.TreeView Is tree Then  
                    Dim pt As Point = New Point(e.X, e.Y)  
                    pt = tree.PointToClient(pt)  
                    Dim nodeTarget As TreeNode = tree.GetNodeAt(pt)      
    
                    If nodeTarget IsNot Nothing Then  
                        e.Effect = DragDropEffects.Copy  
                        tree.SelectedNode = nodeTarget  
                    End If  
                End If  
            End If  
        End Sub  
      
        Private Sub Tree_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)  
            Dim tree As TreeView = CType(sender, TreeView)  
            Dim pt As Point = New Point(e.X, e.Y)  
            pt = tree.PointToClient(pt)  
            Dim nodeTarget As TreeNode = tree.GetNodeAt(pt)  
            Dim nodeSource As TreeNode = CType(e.Data.GetData(GetType(TreeNode)), TreeNode)  
            nodeTarget.Nodes.Add(CType(nodeSource.Clone(), TreeNode))  
            nodeTarget.Expand()  
        End Sub  
    

    Result of my test:
    71410-gif.gif
    Hope it could be helpful.

    Besides, if you need further assistance, please let me know.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. S.Sengupta 15,511 Reputation points MVP
    2021-02-23T13:24:00+00:00

    See here

    and this thread

    it can be used to drag nodes from tree to another one but any one can customize it to be in the same tree