Treeview (auto)select last node entry

In visual basic.net (I'm actually using SharpDevelop; same code), how does one automatically select the last node added to a treeview regardless of placement?

Let's say I have several Root Nodes and I choose the middle one (say, the second root node).  If I were to add a child node to the second root node, I would like it to automatically select the newly added node.

How would one accomplish this?

Example:

Root 1
Root 2
     Sub Added Now <-I want to automatically select this because it was just added
Root 3
     Sub Added A Long Time Ago
Root 4
Sub Button2Click(ByVal sender As Object, ByVal e As EventArgs)
Dim node as TreeNode
node = treeview1.Nodes.Add(str)
'code goes here to automatically select the node which was just added in the previous line of code.'
End Sub

Open in new window

LVL 1
psyfectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
treeview1.SelectedNode = node
0
AUmidhCommented:
Dim inNode as TreeNode=New TreeNode("New Node")
Dim rNode as TreeNode=treeView1.Nodes(rootNodeIndes)
rNode.Nodes.Add(inNode)
treeview1.selectedNode=inNode
MessageBox.Show(treeView1.SelectedNode.Text)
0
psyfectAuthor Commented:
Well those work for the root nodes.  I'm suspecting that when you add a child node to a root node it wont select the child because the root is not expanded.  My solution requires a child node be selected.

If it is possible to select the child node without expanding the root, that would be great, but if you must expand the root node that's fine, I just don't know how to do it.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

AUmidhCommented:
when you insert a node into the root node it automatically expand the root node.  and apply the above code it insert a new node and select that inserted node. you can validate the selected node by .

MessageBox.Show(treeView1.SelectedNode.Text)
0
psyfectAuthor Commented:
It's not automatically expanding the root node for me and when I the child to the root it selects the root and not the child like wanted.  Here's a short version of the code:
	Sub SubjectToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
		on error goto err
		Dim stri As String, node As TreeNode
		stri = inputbox("Subject")
		If stri <> "" Then
		node = treeview1.SelectedNode
		node.Nodes.Add(stri)
		treeview1.SelectedNode = node
		End If
		Exit Sub
		err:
		msgbox ("Please select a Subject.")
	End Sub

Open in new window

0
psyfectAuthor Commented:
The problem appears to be here:
node = treeview1.SelectedNode
node.Nodes.Add("Child Node of Root 'Happy'")
treeview1.SelectedNode = node
msgbox (treeview1.selectednode.text)

The third line seems to reference the first line by returning that root nodes text "Happy" instead of "Child Node of Root 'Happy'".  I could add: treeview1.SelectedNode = node.FirstNode but that wont reference the child recently entered.  It instead references the first child every time.
Visual:
 
Happy
  Previously created child
  Child Node of Root Happy
 
treeview1.selectednode = node.FirstNode returns "Previously created child"
treeview1.selectednode = node returns "Happy"
 
I need it to return the last entered child node every time.

Open in new window

0
ReecioCommented:
How about...
treeview1.FindNode(node.valuepath).Select()
that should work no matter where u put it!?
0
ReecioCommented:
Oops, keeping getting mixed up with ASP.Net and VB.Net.
For VB all you needis....

Sub Button2Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim iNode As New TreeNode
        iNode = TreeView1.Nodes.Add(Str)
        TreeView1.SelectedNode = iNode
End Sub
0
psyfectAuthor Commented:
That's what I'm currently using and it keeps selecting the root node.  Remember that I'm adding a child node.  Alter your code to:

Sub Button2Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim iNode As New TreeNode
        iNode = TreeView1.Nodes.Add(Str)
        TreeView1.SelectedNode = iNode
End Sub

Sub Button3Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim iNode As New TreeNode
        iNode = TreeView1.SelectedNode
        iNode.Nodes.Add(stri)
        TreeView1.SelectedNode = iNode
End Sub

Let's say button 2 adds a root node entitled "Root 1" and button 3 adds a child node to "Root 1" called "Child 1."  Button 3 will select "Root 1" instead of (expanding and?) selecting "Child 1."

That explanation may make more sense.  Thanks for all the help so far.  Hopefully we can figure this out.
0
AUmidhCommented:
Dim inNode as TreeNode=New TreeNode("New Node")
Dim rNode as TreeNode=treeView1.Nodes(rootNodeIndes)
rNode.Nodes.Add(inNode)
treeview1.selectedNode=inNode
MessageBox.Show(treeView1.SelectedNode.Text)

Dim rootNode as TreeNode
Dim childNode as TreeNode
Sub Button2Click(ByVal sender As Object, ByVal e As EventArgs)
        rootNode=New TreeNode("Root")
        TreeView1.Nodes.Add(rootNode)
End Sub

Sub Button3Click(ByVal sender As Object, ByVal e As EventArgs)
        childNode=New TreeNode("Child")
        rootNode.Nodes.Add(childNode)
        rootNode.Expand()
        TreeView1.SelectedNode=childNode
End Sub
0
AUmidhCommented:
woh sorry!
Use only the following code...


Dim rootNode as TreeNode
Dim childNode as TreeNode
Sub Button2Click(ByVal sender As Object, ByVal e As EventArgs)
        rootNode=New TreeNode("Root")
        TreeView1.Nodes.Add(rootNode)
End Sub

Sub Button3Click(ByVal sender As Object, ByVal e As EventArgs)
        childNode=New TreeNode("Child")
        rootNode.Nodes.Add(childNode)
        rootNode.Expand()
        TreeView1.SelectedNode=childNode
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ReecioCommented:
Maybe you should just give it a key? think you use New TreeNode (<key>,<str>) to set one.
Since the key has to be unique (think it sets the the name of the node?), should in theory work?
0
ReecioCommented:
Oh, should of said you can use New TreeNode(Treeview1.nodes.count,<str>) for ease
0
psyfectAuthor Commented:
I had to alter the code a bit but this answer inspired the course to a conclusion and also included the 'expand' code.  As far as adding a key, to the best of my knowledge they removed the key feature in vb.net (unfortunate, but may have been replaced with an additional function).
0
psyfectAuthor Commented:
It's my understanding they removed the key function from vb when releasing .net.  I thought I had found a solution to that issue, but it was a while ago so I don't remember if there was a resolution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.