Results 1 to 7 of 7

Thread: [RESOLVED] VB6 Treeview -Coding selective expansion of nodes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2017
    Posts
    20

    Resolved [RESOLVED] VB6 Treeview -Coding selective expansion of nodes

    I'm new to using Treeview so please excuse my ignorance for asking this question.

    Say I have a Treeview like this...

    Food
    +--Beverages
    ----------Water
    ----------Soda Pop
    +--Fruits
    ----------Apples
    ----------Oranges
    ----------Peaches
    +--Meats
    ----------Beef
    ----------Chicken

    How do I code it so when I click on Food it just shows Beverages, Fruits, and Meats but not the Water, Soda Pop, Apples, and other sub-items?

    Also, I need to be able to select an item from code, for example,
    TreeView1.SelectedItem = TreeView1.Nodes.Item("Food, Fruits, Apples") but have the Treeview expand just that particular branch, without expanding all the other Food branches to show Water, Beef, and etc.

    I've seen examples elsewhere using recursive loops with MSComctlLib.Node but don't know how to apply that to my application. Any help is appreciated.

  2. #2
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,255

    Re: VB6 Treeview -Coding selective expansion of nodes

    This should work if I understood the question correctly:

    Code:
    Private Sub TreeView1_NodeClick(ByVal node As MSComctlLib.node)
    Dim thisNode As MSComctlLib.node
      
      Set thisNode = node.Child
      Do
        thisNode.Expanded = False
        If thisNode = thisNode.LastSibling Then Exit Do
        Set thisNode = thisNode.Next
      Loop Until False
      
      
    End Sub
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2017
    Posts
    20

    Re: VB6 Treeview -Coding selective expansion of nodes

    Thanks, I appreciate your reply. That does do what I wanted! But I have two questions about it. One, this works when clicking on the text part of the Tree, like Food, Fruits, or Meats. Is there a way to make the same thing happen when I click on the "+" signs too?

    And secondly, if I click on a node that is the last child, like "Apples," I get a "variable not set" error because thisNode = Nothing. I've tried to screen that out using If thisNode = Nothing Then Exit Sub, but that gives an "invalid use of object" error. So how do I avoid this problem?

    Quote Originally Posted by ColinE66 View Post
    This should work if I understood the question correctly:

    Code:
    Private Sub TreeView1_NodeClick(ByVal node As MSComctlLib.node)
    Dim thisNode As MSComctlLib.node
      
      Set thisNode = node.Child
      Do
        thisNode.Expanded = False
        If thisNode = thisNode.LastSibling Then Exit Do
        Set thisNode = thisNode.Next
      Loop Until False
      
      
    End Sub

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

    Re: VB6 Treeview -Coding selective expansion of nodes

    The treeview has an Expand event. The node where you hit the "+" will be referenced in that event. Know that the event is raised each time a node is expanded. If using the loop above, each child node you expanded should be raised in that Expand event. This may result in something like a recursive expand -- not tested it myself; just thinking out loud.

    Regarding your error, check first if passed node has children, i.e.,
    If node.Children = 0 Then Exit Sub
    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}

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2017
    Posts
    20

    Re: VB6 Treeview -Coding selective expansion of nodes

    Thanks! You've been very helpful. I now have it working they way I wanted and your information gave me a better overall understanding of the subject.

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

    Re: VB6 Treeview -Coding selective expansion of nodes

    Tip: When using a control you are not completely familiar with, sometimes it can be useful to put debug.print statements in each of the events. Then as you run the project, you can see which events are fired and in what order. For example, in the NodeClick event, you could've added: Debug.Print "NodeClick" and in the Expand event: Debug.Print "Expand". Repeat for all other events you are curious about.

    Now when running the project, ensure the immediate window is open (CTRL+G) then run your project. Click around and watch the stuff being printed to the immediate/debug window.

    Since you have this resolved, remember to mark the thread as so. Use the "Thread Tools" menu near top of your first post.
    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
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,255

    Re: VB6 Treeview -Coding selective expansion of nodes

    Quote Originally Posted by LaVolpe View Post
    The treeview has an Expand event.
    Ha! Been so long since I used the TreeView control I forgot about that event. That is, of course, a better location to handle the collapsing of child nodes...
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

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