am writing code that walks through treeview. i am trying to move through the treeview i.e.
when i click next it moves from parent to child if in parent, another next moves to next child, another next to next child, if at the end of the child, it moves to next parent until the end. please see the attached picture
but cant figure it out.
any help
Programming is all about good logic. Spend more time here
Try this. Tested on this type of structure & worked well. Nodes were not added in topographical order either; just like in real life where the child nodes may be added well after the parent nodes have been added.
1. Use a timer for testing, set its interval about 1/2 second
2. Ensure TreeView1.HideSelection = False
3. Ensure the root node is selected first then enable the timer
Code:
Private Sub Timer1_Timer()
Dim tNode As Node
Set tNode = TreeView1.SelectedItem
If tNode.Children Then ' has child nodes; move to 1st child
Set tNode = tNode.Child.FirstSibling
ElseIf tNode.Next Is Nothing Then ' gotta move up level(s)
Set tNode = tNode.Parent
Do Until tNode Is Nothing ' if Nothing, then done
If Not tNode.Next Is Nothing Then
Set tNode = tNode.Next
Exit Do
End If
Set tNode = tNode.Parent ' move up again
Loop
Else
Set tNode = tNode.Next ' move to next sibling
End If
If tNode Is Nothing Then
' keep looping around & around until you stop the timer
Set TreeView1.SelectedItem = TreeView1.Nodes(1).Root
Else
Set TreeView1.SelectedItem = tNode
End If
End Sub
The code is set to go around & around from top to bottom, again and again. While it is running, you can click on any node to set the current starting position.
Last edited by LaVolpe; Jan 26th, 2010 at 04:28 PM.
Reason: made it a bit shorter
Insomnia is just a byproduct of, "It can't be done"
Sorry to be dense, but Bill Clinton would have asked exactly what exactly "it" is...
when i click next it moves
The highlight, the selection or the node? The up/down arrow keys work nicely for selection, and Volpe's code appears sufficient for emulating their behavior (...although if you are relying on mouse imput, why not just select the desired node with the mouse?)
This leads me to wonder if you might want to move the node up and down. That's not so easy, but the attached demo shows that it is possible. As written, however, nodes won't leave their original family -- but there's no reason why that couldn't be worked out as well.
Sorry to be dense, but Bill Clinton would have asked exactly what exactly "it" is...
The highlight, the selection or the node? The up/down arrow keys work nicely for selection, and Volpe's code appears sufficient for emulating their behavior (...although if you are relying on mouse imput, why not just select the desired node with the mouse?)
This leads me to wonder if you might want to move the node up and down. That's not so easy, but the attached demo shows that it is possible. As written, however, nodes won't leave their original family -- but there's no reason why that couldn't be worked out as well.
What i simply want is to move the treeview selection from parent -> child -> subchild -> parent and so on.
Am not moving the node, just selection. when i select anything child or parent in the treeview, I display associated data in a textbox.
Got it?
Programming is all about good logic. Spend more time here
1. i don't want to loop a single parent
2. i would want the next selection highlighted and if it has children, expanded
in the down arrow see d code i inserted there
Code:
If ndNode.Children Then ' has child nodes; move to 1st child
Set ndNode = ndNode.Child.FirstSibling
ndNode.EnsureVisible
ndNode.Expanded = True
ElseIf ndNode.Next Is Nothing Then ' gotta move up level(s)
Set ndNode = ndNode.Parent.Next
Else
Set ndNode = ndNode.Next ' move to next sibling
End If
Programming is all about good logic. Spend more time here
I'm not following, can you use the Image in post #2 above and describe the scenario with that image? Example> Start at "Mom" move to ... then to if ...
Insomnia is just a byproduct of, "It can't be done"
I'm not following, can you use the Image in post #2 above and describe the scenario with that image? Example> Start at "Mom" move to ... then to if ...
on pressing down arrow button, program starts (and selects) mom if no current selection or from current selection.
on pressing down again, program moves selection to first child if mom has children or moves to aunt if no children.
if down is pressed again it moves selection to next child, or child children if it has any and so on.
if down is pressed if at end of child, it moves to aunt or next parent
Hope you got it now
thanks 4 ur help
Programming is all about good logic. Spend more time here
:: on pressing down again, program moves selection to first child if mom has children or moves to aunt if no children.
-- the code I posted does that, no?
:: if down is pressed again it moves selection to next child, or child children if it has any and so on.
-- the code I posted does that, no?
:: if down is pressed if at end of child, it moves to aunt or next parent
-- the code I posted does that, no?
So I guess the better question is what specific scenario failed with the posted code?
I'd have to recreate the tree to play with it, but I am assuming you already tried what I posted with a timer? If so, you will have also seen that when moving to an unexpanded node, the node self-expands.
Insomnia is just a byproduct of, "It can't be done"
Hmmm, are you placing that code in the treeview's KeyDown event?
If so, add the exact code I posted (changing Treeview1 to your treeview's name) and append this line at the end:
:: KeyCode = 0
Insomnia is just a byproduct of, "It can't be done"
:: on pressing down again, program moves selection to first child if mom has children or moves to aunt if no children.
-- the code I posted does that, no?
:: if down is pressed again it moves selection to next child, or child children if it has any and so on.
-- the code I posted does that, no?
:: if down is pressed if at end of child, it moves to aunt or next parent
-- the code I posted does that, no?
So I guess the better question is what specific scenario failed with the posted code?
I'd have to recreate the tree to play with it, but I am assuming you already tried what I posted with a timer? If so, you will have also seen that when moving to an unexpanded node, the node self-expands.
Thanks for the quick respons, but your code uses a timer, and it loops the treeview. what i want if you could help me out is to do the moving of selection only when i click down or up arrow
you code works fine, but can seem to figure it out for my solution
thanks again
Programming is all about good logic. Spend more time here
Hmmm, are you placing that code in the treeview's KeyDown event?
If so, add the exact code I posted (changing Treeview1 to your treeview's name) and append this line at the end:
:: KeyCode = 0
am not placing ther code in the treeview keydown event. if you look at post #1, you'll see in that picture that i have two blue arrow button (up and down). its actually a toolbar. so i place the code in the toolbar button press event
Programming is all about good logic. Spend more time here
Ok, post your complete click/keydown event for the down arrow please. The code I posted should still work just fine, whether a timer is calling it or your click event is calling it, makes no difference. I think it is something else.
Insomnia is just a byproduct of, "It can't be done"
Ok, your up-arrow code will be different. Here's something that I think will work.
Code:
Set tNode = TreeView1.SelectedItem
If tNode.Previous Is Nothing Then
Set tNode = tNode.Parent
Else
Set tNode = tNode.Previous
Do Until tNode Is Nothing
If tNode.Children Then
Set tNode = tNode.Child.LastSibling
Else
Exit Do
End If
Loop
End If
If Not tNode Is Nothing Then Set TreeView1.SelectedItem = tNode
Insomnia is just a byproduct of, "It can't be done"
@LaVolpe thanks i tried your code. the "up" works but, it does not expand the highlighted
node. the "down" dosent work. pls can you hlp me one more time.
vb Code:
Case "down"
Set ndNode = trvMain.SelectedItem
If ndNode.Next Is Nothing Then
Set ndNode = ndNode.Parent
Else
Set ndNode = ndNode.Next
Do Until ndNode Is Nothing
If ndNode.Children Then
Set ndNode = ndNode.Child.LastSibling
Else
Exit Do
End If
Loop
End If
If Not ndNode Is Nothing Then Set trvMain.SelectedItem = ndNode
Case "up"
Set ndNode = trvMain.SelectedItem
If ndNode.Previous Is Nothing Then
Set ndNode = ndNode.Parent
Else
Set ndNode = ndNode.Previous
Do Until ndNode Is Nothing
If ndNode.Children Then
Set ndNode = ndNode.Child.LastSibling
Else
Exit Do
End If
Loop
End If
If Not ndNode Is Nothing Then Set trvMain.SelectedItem = ndNode
Programming is all about good logic. Spend more time here
Well, take no offense, but I already provided the code that appears to work for the Down method in post #2. If you don't want to use it, that is up to you.
Regarding the "up" not expanding? I couldn't reproduce the problem on my end, but it could be this simple tweak?
:: before Set ndNode = ndNode.Child.LastSibling
:: add ndNode.Expanded = True
Insomnia is just a byproduct of, "It can't be done"