Solved

moving nodes in a treeview

Posted on 2004-08-17
3
1,013 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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