Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET TreeView - dynamically selecting a node after it has been inserted or moved?

I'm working on a VB.NET 2010 project which features a treeview control. The first thing I'm trying to figure out is how to insert a new node right after the currently selected node, and then make that newly inserted node the selected node. I can insert the new node no problem, but I can't figure out how to make it the "selected" node. The commented line below is the part I'm getting hung up on.

Private Sub AddNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNode.Click

    If Not treeview1.SelectedNode Is Nothing Then
        treeview1.Nodes.Insert(treeview1.SelectedNode.Index + 1, TextBox1.Text)
        treeview.SelectedNode = treeview1.Nodes.Item(treeview1.SelectedNode.Index + 1) ' <-- I thought this would work, but it doesn't
    End If

End Sub

Secondly, I am using the below code to move a selected node up (in relation to other nodes). That works fine, but similar to the problem above, I can't figure out how to keep that node as the "selected" node after it has been moved.

Private Sub NodeUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NodeUp.Click

    Dim CurrentIndex As Integer = treeview1.SelectedNode.Index
    Dim CurrentNode As TreeNode = treeview1.SelectedNode
    treeview1.SelectedNode.Remove()
    treeview1.Nodes.Insert(CurrentIndex - 1, CurrentNode)

End Sub

This has to be simple, but I'm wracking my brain trying to figure out how, so I would appreciate a little bit of insight here.

Thanks!

like image 557
NotQuiteThereYet Avatar asked May 11 '13 22:05

NotQuiteThereYet


People also ask

How do I know if TreeView node is selected?

To determine which TreeView node was clickedUse the EventArgs object to return a reference to the clicked node object. Determine which node was clicked by checking the TreeViewEventArgs class, which contains data related to the event.

Which property in the TreeView control represent the node?

The key properties of the TreeView control are Nodes and SelectedNode. The Nodes property contains the list of top-level nodes in the tree view. The SelectedNode property sets the currently selected node.

Which method is used to add child node in TreeView control in VB net?

If you want to add the child nodes to a particluar parent node, the idea is to add the child nodes to their parent node by using the parent. node. add() method. You can create any number of child like this.

What is the purpose of VB Net TreeView control?

The TreeView control is used to display hierarchical representations of items similar to the ways the files and folders are displayed in the left pane of the Windows Explorer. Each node may contain one or more child nodes. Let's click on a TreeView control from the Toolbox and place it on the form.


1 Answers

For setting the selected node in a TreeView you call TreeView.SelectedNode to the TreeNode you want to select.

Now that we've established that, down to your examples:

When you call TreeView.Nodes.Insert using the overload you have (integer, string) you actually get a TreeNode object returned to you. So if you change your sample to

Private Sub AddNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNode.Click

    If Not treeview1.SelectedNode Is Nothing Then
        Dim NewNode as TreeNode = treeview1.Nodes.Insert(treeview1.SelectedNode.Index + 1, TextBox1.Text)
        treeview.SelectedNode = NewNode
    End If

End Sub

then it should select the node you just created.

Your second example just needs to have one line added to it:

Private Sub NodeUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NodeUp.Click

    Dim CurrentIndex As Integer = treeview1.SelectedNode.Index
    Dim CurrentNode As TreeNode = treeview1.SelectedNode
    treeview1.SelectedNode.Remove()
    treeview1.Nodes.Insert(CurrentIndex - 1, CurrentNode)
    treeview1.SelectedNode = CurrentNode

End Sub

This is all working from brain compiler at the moment as I don't have access to Visual Studio to test it, so please let me know if you have any problems.

like image 51
Adrian Avatar answered Sep 28 '22 09:09

Adrian