Solved

moving nodes in a treeview

Posted on 2004-08-17
3
1,007 Views
Last Modified: 2008-02-01
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
Comment
Question by:chlade
3 Comments
 
LVL 21

Accepted Solution

by:
tovvenki earned 500 total points
ID: 11827578
Hi,
check whether the following KB helps you
http://support.microsoft.com/default.aspx?scid=kb;EN-US;307967

Regards,
venki
0
 
LVL 3

Expert Comment

by:Scoobyed
ID: 11838673
0
 

Author Comment

by:chlade
ID: 11841811
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question