-
Apr 23rd, 2017, 01:29 AM
#1
Thread Starter
Junior Member
[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.
-
Apr 23rd, 2017, 07:14 AM
#2
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
-
Apr 23rd, 2017, 10:18 AM
#3
Thread Starter
Junior Member
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?
Originally Posted by ColinE66
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
-
Apr 23rd, 2017, 11:47 AM
#4
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
-
Apr 23rd, 2017, 01:27 PM
#5
Thread Starter
Junior Member
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.
-
Apr 23rd, 2017, 01:57 PM
#6
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.
-
Apr 23rd, 2017, 01:59 PM
#7
Re: VB6 Treeview -Coding selective expansion of nodes
Originally Posted by LaVolpe
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|