Link to home
Start Free TrialLog in
Avatar of psyfect
psyfect

asked on

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

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

treeview1.SelectedNode = node
Avatar of AUmidh
AUmidh

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)
Avatar of psyfect

ASKER

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.
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)
Avatar of psyfect

ASKER

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

Avatar of psyfect

ASKER

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

How about...
treeview1.FindNode(node.valuepath).Select()
that should work no matter where u put it!?
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
Avatar of psyfect

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of AUmidh
AUmidh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Oh, should of said you can use New TreeNode(Treeview1.nodes.count,<str>) for ease
Avatar of psyfect

ASKER

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).
Avatar of psyfect

ASKER

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.