-
Dec 27th, 2011, 02:00 PM
#1
Thread Starter
Lively Member
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?
-
Dec 27th, 2011, 02:39 PM
#2
Frenzied Member
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
-
Dec 27th, 2011, 06:03 PM
#3
Thread Starter
Lively Member
Re: Moving a TreeView node up/down at run-time?
Originally Posted by incidentals
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?
-
Dec 27th, 2011, 06:49 PM
#4
Member
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.
-
Dec 28th, 2011, 10:30 AM
#5
Thread Starter
Lively Member
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.
-
Dec 24th, 2017, 08:39 PM
#6
New Member
Re: Moving a TreeView node up/down at run-time?
Originally Posted by BMAN645
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...
-
Dec 24th, 2017, 09:54 PM
#7
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:
Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim root As TreeNode TreeView1.HideSelection = False For j = 1 To 9 root = New TreeNode With {.Text = j.ToString} TreeView1.Nodes.Add(root) For k = 1 To 9 root.Nodes.Add(New TreeNode With {.Text = j.ToString & "." & k.ToString}) Next Next End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MoveNodeUp(TreeView1.SelectedNode) End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click MoveNodeDown(TreeView1.SelectedNode) End Sub Private Sub MoveNodeUp(ByVal tvnNodeToMove As TreeNode) Dim tvnPrevNode As TreeNode = tvnNodeToMove.PrevNode If tvnPrevNode IsNot Nothing Then TreeView1.BeginUpdate() tvnNodeToMove.Remove() If tvnPrevNode.Parent Is Nothing Then ' Move root TreeView1.Nodes.Insert(tvnPrevNode.Index, tvnNodeToMove) Else tvnPrevNode.Parent.Nodes.Insert(tvnPrevNode.Index, tvnNodeToMove) End If TreeView1.SelectedNode = tvnNodeToMove TreeView1.EndUpdate() End If End Sub Private Sub MoveNodeDown(ByVal tvnNodeToMove As TreeNode) Dim tvnNextNode As TreeNode = tvnNodeToMove.NextNode If tvnNextNode IsNot Nothing Then TreeView1.BeginUpdate() tvnNodeToMove.Remove() If tvnNextNode.Parent Is Nothing Then ' Move root TreeView1.Nodes.Insert(tvnNextNode.Index + 1, tvnNodeToMove) Else tvnNextNode.Parent.Nodes.Insert(tvnNextNode.Index + 1, tvnNodeToMove) End If TreeView1.SelectedNode = tvnNodeToMove TreeView1.EndUpdate() End If End Sub End Class
-
Dec 25th, 2017, 12:02 AM
#8
New Member
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?
-
Dec 25th, 2017, 12:48 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|