Results 1 to 9 of 9

Thread: Moving a TreeView node up/down at run-time?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    126

    Moving a TreeView node up/down at run-time?

    Hey guys,

    Spent most of the day yesterday trying to figure this out but no luck so far. I would have thought that there would be an easy way to do this, but after spending hours Google'ing the subject, I could not find a single working example to accomplish this.

    Anyways, here's what I'm trying to do...

    I have a tree view with up/down buttons. The up/down buttons should move the selected node up or down in relation to the other nodes.

    For example, if the nodes in the tree view start out in this order...

    Node1
    Node2
    Node3

    ... then if the user selects 'Node2' and clicks the 'Down' button, the nodes should then be in this order...

    Node1
    Node3
    Node2

    Simple, right? It would be simple if the node's 'index' property was not read-only. If that was the case, then I could just do something like this...

    Code:
    Private Sub UpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpButton.Click
    
            TreeView1.SelectedNode.Index = TreeView1.SelectedNode.Index - 1
    
        End Sub
    
        Private Sub DownButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownButton.Click
    
            TreeView1.SelectedNode.Index = TreeView1.SelectedNode.Index + 1
    
        End Sub
    But the 'Index' property is read-only, so I have no clue how to accomplish this (nor does anyone on the first 5 pages of Google search results, lol).

    Any ideas?

  2. #2
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Moving a TreeView node up/down at run-time?

    you will probably need to build a second tree that takes the place of the first when complete

    just a thought

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    126

    Re: Moving a TreeView node up/down at run-time?

    Quote Originally Posted by incidentals View Post
    you will probably need to build a second tree that takes the place of the first when complete

    just a thought
    Not sure I'm following your logic there. What would be the point of building a 2nd tree?

  4. #4
    Member
    Join Date
    Aug 2004
    Location
    Sao Paulo / Brazil
    Posts
    38

    Re: Moving a TreeView node up/down at run-time?

    Hi,
    here is me code for a ListView, its almost the same as the treeview (just uses the same level)

    Code:
        Public Sub UpSelected()
    
            If Me.SelectedItems.Count = 0 Then
                Exit Sub
            End If
    
            If Me.SelectedIndices(0) = 0 Then
                Me.Focus()
                Exit Sub
            End If
    
            Dim NewItem As ListViewItem = CType(Me.SelectedItems(0).Clone, ListViewItem)
            Dim PreviousItem As ListViewItem = CType(Me.Items(Me.SelectedIndices(0) - 1), ListViewItem)
    
            Me.Items.Insert(Me.SelectedIndices(0) - 1, NewItem)
            Me.SelectedItems(0).Remove()
            NewItem.Selected = True
    
            Me.Focus()
    
        End Sub
    
        Public Sub DownSelected()
    
            If Me.SelectedItems.Count = 0 Then
                Exit Sub
            End If
    
            If Me.SelectedIndices(0) + 1 >= Me.Items.Count Then
                Me.Focus()
                Exit Sub
            End If
    
            Dim NewItem As ListViewItem = CType(Me.SelectedItems(0).Clone, ListViewItem)
            Dim PreviousItem As ListViewItem = CType(Me.Items(Me.SelectedIndices(0) + 1), ListViewItem)
    
            Me.Items.Insert(Me.SelectedIndices(0) + 2, NewItem)
            Me.SelectedItems(0).Remove()
            NewItem.Selected = True
    
            Me.Focus()
    
        End Sub
    --------------------------------------
    All your base are belong to us.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    126

    Re: Moving a TreeView node up/down at run-time?

    Thanks for the example cbuosi, but I have no idea how to apply that to a tree view.

    I would have thought that moving a node up or down would be something that a lot of people would want to do with a tree view control. Strange that there does not appear to be a simple way to do it.

  6. #6
    New Member
    Join Date
    Dec 2017
    Posts
    3

    Re: Moving a TreeView node up/down at run-time?

    Quote Originally Posted by BMAN645 View Post
    Thanks for the example cbuosi, but I have no idea how to apply that to a tree view.

    I would have thought that moving a node up or down would be something that a lot of people would want to do with a tree view control. Strange that there does not appear to be a simple way to do it.
    It's an old post, but are there answers on this? I searched the site but couldn't find anything. I suppose the idea would be to move the bottom-most displayed treeview node to the top of the display to move down, and to move the top-most displayed node to the bottom to go up a page. I've scoured the web for a code sample but no joy...

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Moving a TreeView node up/down at run-time?

    Start new project, add Button1, Button2 and TreeView1 then Paste the following code

    vb.net Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.  
    6.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    7.         Dim root As TreeNode
    8.         TreeView1.HideSelection = False
    9.         For j = 1 To 9
    10.             root = New TreeNode With {.Text = j.ToString}
    11.             TreeView1.Nodes.Add(root)
    12.             For k = 1 To 9
    13.                 root.Nodes.Add(New TreeNode With {.Text = j.ToString & "." & k.ToString})
    14.             Next
    15.         Next
    16.     End Sub
    17.  
    18.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    19.         MoveNodeUp(TreeView1.SelectedNode)
    20.     End Sub
    21.  
    22.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    23.         MoveNodeDown(TreeView1.SelectedNode)
    24.     End Sub
    25.  
    26.     Private Sub MoveNodeUp(ByVal tvnNodeToMove As TreeNode)
    27.         Dim tvnPrevNode As TreeNode = tvnNodeToMove.PrevNode
    28.         If tvnPrevNode IsNot Nothing Then
    29.             TreeView1.BeginUpdate()
    30.             tvnNodeToMove.Remove()
    31.             If tvnPrevNode.Parent Is Nothing Then ' Move root
    32.                 TreeView1.Nodes.Insert(tvnPrevNode.Index, tvnNodeToMove)
    33.             Else
    34.                 tvnPrevNode.Parent.Nodes.Insert(tvnPrevNode.Index, tvnNodeToMove)
    35.             End If
    36.             TreeView1.SelectedNode = tvnNodeToMove
    37.             TreeView1.EndUpdate()
    38.         End If
    39.     End Sub
    40.  
    41.     Private Sub MoveNodeDown(ByVal tvnNodeToMove As TreeNode)
    42.         Dim tvnNextNode As TreeNode = tvnNodeToMove.NextNode
    43.         If tvnNextNode IsNot Nothing Then
    44.             TreeView1.BeginUpdate()
    45.             tvnNodeToMove.Remove()
    46.             If tvnNextNode.Parent Is Nothing Then ' Move root
    47.                 TreeView1.Nodes.Insert(tvnNextNode.Index + 1, tvnNodeToMove)
    48.             Else
    49.                 tvnNextNode.Parent.Nodes.Insert(tvnNextNode.Index + 1, tvnNodeToMove)
    50.             End If
    51.             TreeView1.SelectedNode = tvnNodeToMove
    52.             TreeView1.EndUpdate()
    53.         End If
    54.     End Sub
    55.  
    56. End Class



  8. #8
    New Member
    Join Date
    Dec 2017
    Posts
    3

    Re: Moving a TreeView node up/down at run-time?

    4x2y- Thanks for the quick response!

    One other thing I'm trying to do (not the same as the original thread so I might start a new one).

    I have a treeview where I'm trying to provide forward and back buttons, so that the displayed portion of the treeview will page forward or backwards. This would be an alternative way to page through a large treeview as opposed to using the vertical scroll bar. I didn't see any methods associated with moving the displayed portion of the treeview.

    Any ideas?

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Moving a TreeView node up/down at run-time?

    TreeView is showing hierarchic structure, I can't imagine how it can can be splitted into pages as you describe!

    Please open new thread



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width