Link to home
Start Free TrialLog in
Avatar of alsam
alsam

asked on

Drag and drop between listbox and treeview

HI,
I have win form with TreeView control on itself...I made drag and drop working within treeview very well...Since the treeview hierarchy is so large (about 700 chaild nodes) i got request from users to try to make simplyfied moving chiled nodes from one parent to another in treeView hierarchy ... So I have decided to introduce ListView Control on the same form which will be loaded with child nodes related to selected parent in treeView hierarchy...
Now I face with big trouble how to make working this drag and drop from listView to treeview (i've been searching for the solution on the web for 2 days but with no success)...
Also, when I drag and drop listvie items on new location (on new parent) in treeView the same should be removed from the old position within hierarchy...
In addition please find drag and drop subs which I use for movig treView items in treeview it self....New ListView is marked as ListView1
Public Sub TreeView1_ItemDrag(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.ItemDragEventArgs) _
        Handles TreeView1.ItemDrag

        m_tnSource = CType(e.Item, TreeNode)
        DoDragDrop(e.Item, DragDropEffects.Move)


    End Sub

    Public Sub TreeView1_DragEnter(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DragEventArgs) _
        Handles TreeView1.DragEnter


        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
            True) Then

            e.Effect = DragDropEffects.Move
        Else

            e.Effect = DragDropEffects.None
        End If

    End Sub

    Public Sub TreeView1_DragOver(ByVal sender As System.Object, _
         ByVal e As System.Windows.Forms.DragEventArgs) _
        Handles TreeView1.DragOver


        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
               True) = False Then Exit Sub


        Dim selectedTreeview As TreeView = CType(sender, TreeView)


        Dim pt As Point = _
            CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
        Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)


        If Not (selectedTreeview.SelectedNode Is targetNode) Then

            selectedTreeview.SelectedNode = targetNode


            Dim dropNode As TreeNode = _
                CType(e.Data.GetData("System.Windows.Forms.TreeNode"),  _
                TreeNode)

            Do Until targetNode Is Nothing
                If targetNode Is dropNode Then
                    e.Effect = DragDropEffects.None
                    Exit Sub
                End If
                targetNode = targetNode.Parent
            Loop
        End If


        e.Effect = DragDropEffects.Move


    End Sub

    Public Sub TreeView1_DragDrop(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.DragEventArgs) _
       Handles TreeView1.DragDrop

        Dim pt As Point
        Dim tnDestination As TreeNode
        Dim tnNew As TreeNode
        Dim tnSourceParent As TreeNode

        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then


            pt = TreeView1.PointToClient(New Point(e.X, e.Y))
            tnDestination = TreeView1.GetNodeAt(pt)

            If CTree.IsDropAllowed(m_tnSource, tnDestination) = True Then
                tnSourceParent = m_tnSource.Parent

                tnNew = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
                tnDestination.Nodes.Add(CType(tnNew.Clone, TreeNode))
                tnDestination.ExpandAll()
                tnNew.Remove()

            Else
                
                MsgBox("The dragged item cannot be dropped here because" & _
                       "doing so will create a circular reference.", _
                       MsgBoxStyle.Exclamation)
            End If

        End If

    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of omegaomega
omegaomega
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alsam
alsam

ASKER

Hi,
this is clear A+
Thank you for your time and effort...