Results 1 to 17 of 17

Thread: Walk through treeview. How?

  1. #1

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Walk through treeview. How?

    Hello guys,

    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
    Attached Images Attached Images  
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Walk through treeview. How?

    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.
    Name:  untitled.GIF
Views: 1348
Size:  3.3 KB
    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Member
    Join Date
    Dec 2009
    Posts
    45

    Re: Walk through treeview. How?

    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.
    Attached Files Attached Files

  4. #4

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Walk through treeview. How?

    Quote Originally Posted by FredZone View Post
    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


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  5. #5

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Walk through treeview. How?

    @LaVolpe Thanks

    but can u help me one more time

    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


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Walk through treeview. How?

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Walk through treeview. How?

    Quote Originally Posted by LaVolpe View Post
    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


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Walk through treeview. How?

    Still got a misconnect between you & me.

    :: From Mom

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

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Walk through treeview. How?

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Walk through treeview. How?

    Quote Originally Posted by LaVolpe View Post
    Still got a misconnect between you & me.

    :: From Mom

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


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Walk through treeview. How?

    I posted just before you. Read my previous reply.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Walk through treeview. How?

    Quote Originally Posted by LaVolpe View Post
    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


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Walk through treeview. How?

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #14

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Walk through treeview. How?

    ok let me do som troubleshooting, i'll get back to you. thanks for your ideas
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  15. #15
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Walk through treeview. How?

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  16. #16

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Walk through treeview. How?

    @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:
    1. Case "down"
    2.             Set ndNode = trvMain.SelectedItem
    3.             If ndNode.Next Is Nothing Then
    4.                 Set ndNode = ndNode.Parent
    5.             Else
    6.                 Set ndNode = ndNode.Next
    7.                 Do Until ndNode Is Nothing
    8.                     If ndNode.Children Then
    9.                         Set ndNode = ndNode.Child.LastSibling
    10.                     Else
    11.                         Exit Do
    12.                     End If
    13.                 Loop
    14.             End If
    15.             If Not ndNode Is Nothing Then Set trvMain.SelectedItem = ndNode
    16.  
    17.         Case "up"
    18.             Set ndNode = trvMain.SelectedItem
    19.             If ndNode.Previous Is Nothing Then
    20.                 Set ndNode = ndNode.Parent
    21.             Else
    22.                 Set ndNode = ndNode.Previous
    23.                 Do Until ndNode Is Nothing
    24.                     If ndNode.Children Then
    25.                         Set ndNode = ndNode.Child.LastSibling
    26.                     Else
    27.                         Exit Do
    28.                     End If
    29.                 Loop
    30.             End If
    31.             If Not ndNode Is Nothing Then Set trvMain.SelectedItem = ndNode
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  17. #17
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Walk through treeview. How?

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width