?
Solved

moving nodes in a treeview

Posted on 2004-08-17
3
Medium Priority
?
1,016 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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

649 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