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
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
treeview1.SelectedNode = node
Dim inNode as TreeNode=New TreeNode("New Node")
Dim rNode as TreeNode=treeView1.Nodes(r ootNodeInd es)
rNode.Nodes.Add(inNode)
treeview1.selectedNode=inN ode
MessageBox.Show(treeView1. SelectedNo de.Text)
Dim rNode as TreeNode=treeView1.Nodes(r
rNode.Nodes.Add(inNode)
treeview1.selectedNode=inN
MessageBox.Show(treeView1.
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.
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. SelectedNo de.Text)
MessageBox.Show(treeView1.
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
ASKER
The problem appears to be here:
node = treeview1.SelectedNode
node.Nodes.Add("Child Node of Root 'Happy'")
treeview1.SelectedNode = node
msgbox (treeview1.selectednode.te xt)
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.
node = treeview1.SelectedNode
node.Nodes.Add("Child Node of Root 'Happy'")
treeview1.SelectedNode = node
msgbox (treeview1.selectednode.te
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.
How about...
treeview1.FindNode(node.va luepath).S elect()
that should work no matter where u put it!?
treeview1.FindNode(node.va
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
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
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.
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(r ootNodeInd es)
rNode.Nodes.Add(inNode)
treeview1.selectedNode=inN ode
MessageBox.Show(treeView1. SelectedNo de.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(rootNo de)
End Sub
Sub Button3Click(ByVal sender As Object, ByVal e As EventArgs)
childNode=New TreeNode("Child")
rootNode.Nodes.Add(childNo de)
rootNode.Expand()
TreeView1.SelectedNode=chi ldNode
End Sub
Dim rNode as TreeNode=treeView1.Nodes(r
rNode.Nodes.Add(inNode)
treeview1.selectedNode=inN
MessageBox.Show(treeView1.
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(rootNo
End Sub
Sub Button3Click(ByVal sender As Object, ByVal e As EventArgs)
childNode=New TreeNode("Child")
rootNode.Nodes.Add(childNo
rootNode.Expand()
TreeView1.SelectedNode=chi
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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.c ount,<str> ) for ease
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).
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.