Link to home
Start Free TrialLog in
Avatar of Jacques Geday
Jacques GedayFlag for Canada

asked on

VB6 - Treeview - Loop thru selected item's children

Hello,

I have a treeview control called tvwDB and it is in the following format

Vessel1
  Jhon
     item1
     item2
     item3
  Mikel
     item19
  Phillip
     item64
     item73
     item102
     item112
     item132

Here is what I want:
the user click or select Jhon or Mikel or Phillip I need to know what is the code that allow me loop thru all the children items of that selected item.

So basically here is what I haveif the user say click on Philip I have
tvwDB.SelectedItem shows Philip
tvwDB.SelectedItem.Children shows 5

I need a way to loop thru all the items of Phillip How to do that ???
I tried tvwDB.SelectedItem.Child it give me item64 and tvwDB.SelectedItem.Child.Next give me item73 but can't move further.

Appreciate your help.
Regards
gowflow
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you will need a recursive function to do this, at least to do it with "short and efficient" code:
http://msdn.microsoft.com/en-us/library/wwc698z7%28v=vs.90%29.aspx

as from there, you would need to clarify what you need to do "with each node" if you need further help
Avatar of Jacques Geday

ASKER

tks angelIII for your input. I am afraid that this is a good link however let me rephrase my request.

I need to simply loop from the selecteditem in the treeview and get all its childs
I simply need a for loop that will get each child item in a variable called BLSelected

something like

For each Item in tvwDB.selecteditem.child
       BLSelected = Item
next Item

for sure this code as is is not working for me do not how to iterate in all the CHILD or children of a selected item !!!

Rgds/gowflow
Let me rephrase...
All the child nodes is a collection...
Not a single object

So, we can create the recursive function to collect all the child nodes into a collection.

Question
What type is bi.selected ?
BLSelected not bi.selected !!!

just a string I simply need the value of the child like in my example I need
BLSelected to be
item64
item73
item102
item112
item132


coz when I loop and I get the value item64 by manipulation it points in my larger sub to a file on disk that is a pdf file this is the name of the file and the purpose of the whole routine is to print that file

So when the user clicks on Phillip I need the routine to pull all the children names and each time I use that name to get the file and print it.

Last thing I do not need to loop thru all the nodes in the treeview as I may have a lot some 1000+ items I only need to loop thru all the children of a specific selecteditem that is returned in tvwDB.SelectedItem

Hope it is clearer now.
Tks
gowflow
Private Sub Form_Load()
   Randomize
   TreeView1.LineStyle = tvwRootLines
   TreeView1.Indentation = 300
   Dim ndRoot As Node
   Dim names() As String
   names = Split("John,Mikel,Phillip", ",")
   Set ndRoot = TreeView1.Nodes.Add(, , , "Vessel")
   For Each person In names
       Dim ndPerson As Node
       Set ndPerson = TreeView1.Nodes.Add(ndRoot, tvwChild, "Person_" & person, person)
       Dim count As Long
       count = GetRandom(1, 5)
       For i = 1 To count
          TreeView1.Nodes.Add ndPerson, tvwChild, , "Item" & GetRandom(1, 100)
       Next i
   Next
   ndRoot.Expanded = True
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
   Dim colItems As String
   If Left(Node.Key, 6) = "Person" Then
      colItems = "Subitems for " & Node.Text
      Dim nd As Node
      Set nd = Node.Child
      Do While Not nd Is Nothing
         colItems = colItems & vbCrLf & nd.Text
         Set nd = nd.Next
      Loop
      MsgBox colItems
   End If
End Sub

'Just for test purpose
Private Function GetRandom(ByVal minValue As Long, ByVal maxValue As Long) As Long
   GetRandom = Int((maxValue - minValue + 1) * Rnd + minValue)
End Function

Open in new window

SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I need a way to loop thru all the items of Phillip How to do that ???
I tried tvwDB.SelectedItem.Child it give me item64 and tvwDB.SelectedItem.Child.Next give me item73 but can't move further.
tks both Ark and AngelIII for your code examples.

@Ark you just quoted my comment on your last post any comment ???

I need to test your code I will only take the part
TreeView1_NodeClick and FetchNodesRecursive as the rest is already done by my code and understand you had it there for testing purposes.

The key issue here as I see it:

Dim nd As Node
      Set nd = Node.Child
      Do While Not nd Is Nothing
         colItems = colItems & vbCrLf & nd.Text
         Set nd = nd.Next
      Loop

so my question is now specific:
in my case Node variable is pointing to
tvwDB.SelectedItem how do I convert this to Node variable ???
Shall I do
Dim Node as Node
Set Node = tvwDB.SelectedItem

Will it work ??
gowflow
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Ark you are correct I do not need Recursion although this could be handy for the future which AngelIII much appreciated your input.

Ark you nailed it right on with your last function it is exactly what I needed to get to.

Tks very much for both your input which was much helpful and much appreciated.
Rgds/gowflow