NotQuiteThereYet
NotQuiteThereYet

Reputation: 497

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!

Upvotes: 6

Views: 43560

Answers (1)

Adrian
Adrian

Reputation: 2875

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.

Upvotes: 6

Related Questions