Solved

moving nodes in a treeview

Posted on 2004-08-17
3
997 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now