Navigation in a Treeview control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In VB.Net:
I have a Treeview control with 3 nodes; the third node has a child node. The
first node is selected. if I use up arrow key; it selects the 3rd node.
How do I select the very last node of the tree which is, in my case, is a
child node 'Child1'?

Parent1
Parent 2
Parent 3
Child1
 
Try this for the keydown event on the tree view:
(i wrote it in c# and converted by hand so they may be a small error or two)

Private Sub treeView1_KeyDown(sender as Object, e as KeyEventArgs)
if (e.KeyCode == Keys.Up) then
if (treeView1.SelectedNode.Parent = null and
treeView1.SelectedNode.PrevVisibleNode = null) then
//find last node
TreeNode lastnode = treeView1.Nodes(treeView1.Nodes.Count - 1)
while (lastnode.Nodes.Count > 0)
lastnode = lastnode.Nodes(lastnode.Nodes.Count - 1)
end while

treeView1.SelectedNode = lastnode
e.Handled = true
End if
End If
End Sub



Ciaran O'Donnell
 
Ciaran,
Thanks, it worked fine.


Ciaran O''Donnell said:
Try this for the keydown event on the tree view:
(i wrote it in c# and converted by hand so they may be a small error or two)

Private Sub treeView1_KeyDown(sender as Object, e as KeyEventArgs)
if (e.KeyCode == Keys.Up) then
if (treeView1.SelectedNode.Parent = null and
treeView1.SelectedNode.PrevVisibleNode = null) then
//find last node
TreeNode lastnode = treeView1.Nodes(treeView1.Nodes.Count - 1)
while (lastnode.Nodes.Count > 0)
lastnode = lastnode.Nodes(lastnode.Nodes.Count - 1)
end while

treeView1.SelectedNode = lastnode
e.Handled = true
End if
End If
End Sub



Ciaran O'Donnell
 
You should mark the question as answered and the post as help full then. It
helps people when they are looking for solutions through the history

Ciaran
 
Back
Top