• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1017
  • Last Modified:

moving nodes in a treeview

I am having trouble finding help in any books or online related to the following:  I have a treeview with many nodes, including many nested levels.  

I want to be able to move nodes up and down throughout the treeview.  Think of Windows Explorer and being able to move a file from one folder to another or move  a subfolder from one folder to another.

Any links or sample code would be great.

I am doing this in VB.NET

Thanks,
Chris
0
chlade
Asked:
chlade
1 Solution
 
tovvenkiCommented:
Hi,
check whether the following KB helps you
http://support.microsoft.com/default.aspx?scid=kb;EN-US;307967

Regards,
venki
0
 
chladeAuthor Commented:
tovvenki, While it wasn't exactly what I was looking for it did help me get going.  Thanks.  

Below is the actual code of my event handlers:

    Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag

        DoDragDrop(e.Item, DragDropEffects.All)

    End Sub

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

        If e.KeyState = 9 Then 'KeyState of 9 means CTRL (8) + Left-Click(1)
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.Move
        End If

    End Sub

    'Select the node under the mouse pointer to indicate the expected drop location.
    Private Sub treeView1_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragOver

        ' Retrieve the client coordinates of the mouse position.
        Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))

        ' Select the node at the mouse position.
        TreeView1.SelectedNode = TreeView1.GetNodeAt(targetPoint)

    End Sub

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

        Dim NewNode As TreeNode
        Dim pt As Point
        Dim DestinationNode As TreeNode

        'e contains the data of the dragged items. See if has a node data structure in it.
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then

            'Get the treeview point
            pt = TreeView1.PointToClient(New Point(e.X, e.Y))

            'Get a handle to the node the source node was dragged onto
            DestinationNode = TreeView1.GetNodeAt(pt)

            'Create a new node based on the data contained in the dragged node
            NewNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)

            'Check destination mode
            If Not DestinationNode.Equals(NewNode) Then

                'If moving to the same parent folder it's already in, then leave it alone
                If NewNode.Parent Is DestinationNode Then Exit Sub

                'If moving to a node that is a child node of that node, then skip
                If DestinationNode.Parent Is NewNode Then Exit Sub

                'Add a clone
                If DestinationNode.Nodes.Count = 0 Then
                    DestinationNode.Parent.Nodes.Insert(DestinationNode.Index, CType(NewNode.Clone, TreeNode))
                Else
                    DestinationNode.Nodes.Add(CType(NewNode.Clone, TreeNode))
                End If

                'Highlight the node that was moved/copied
                TreeView1.SelectedNode = DestinationNode.PrevNode

                'Expand the new node
                DestinationNode.Expand()

                'Remove the original node
                If e.Effect = DragDropEffects.Move Then NewNode.Remove()
            End If
        End If

    End Sub
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now