Results 1 to 5 of 5

Thread: [RESOLVED] TreeView Drag&Drop nodes

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Resolved [RESOLVED] TreeView Drag&Drop nodes

    Please, I'm on the verge of pulling my hair out. What am I doing wrong?
    Basically I need to be able to drag nodes between different parents within a treeview.
    The treeview simply does not acctept the Drop and DragDrop event is not firing.
    I wrote an example code with the same effect (node drag is not working). Wham am I missing?

    Code:
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim root As New TreeNode("ROOT")
            root.Nodes.Add("Child1")
            root.Nodes.Add("Child2")
            root.Nodes.Add("Child3")
            TreeView1.AllowDrop = True
            TreeView1.Nodes.Add(root)
        End Sub
    
        Private Sub TreeView1_DragDrop(sender As Object, e As DragEventArgs) Handles TreeView1.DragDrop
    
        End Sub
    
        Private Sub TreeView1_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles TreeView1.ItemDrag
            DoDragDrop(e.Item, DragDropEffects.Move)
        End Sub
    End Class

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,747

    Re: TreeView Drag&Drop nodes

    Have you tried this sample?

  3. #3

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: TreeView Drag&Drop nodes

    This one works, but I need to drag nodes within a single Treeview. I can't understand why this isn't working.

  4. #4

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: TreeView Drag&Drop nodes

    Got it working, even though I still don't understand why this was necessary. All I needed was to add the DragEnter event handler:
    Code:
    Private Sub TreeView1_DragEnter(sender As Object, e As DragEventArgs) Handles TreeView1.DragEnter
            e.Effect = DragDropEffects.Move
    End Sub

  5. #5
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: [RESOLVED] TreeView Drag&Drop nodes

    I did a project heavy in this type of moving nodes between trees and around on the same trees. I don't have that code handy but use the example that Arnoutdv gave and remember you really aren't just changing the parent of the node but instead notice in the TreeView1.DragDrop event in the example they add a Clone of the node and then remove the actual node. A node cannot belong to 2 parents at the same time. Nor can it belong to 2 trees, though not your problem here.

    Hope that helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width