[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2728
  • Last Modified:

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

0
psyfect
Asked:
psyfect
  • 6
  • 4
  • 4
  • +1
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now