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
LVL 31
gowflowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
0
gowflowAuthor Commented:
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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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 ?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gowflowAuthor Commented:
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
0
ArkCommented:
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

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
well, Ark's suggestion will only give the first level of children, not all sub-children if there are more levels. here the adjusted code:
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 FetchNodesRecursive(ByVal _item As MSComctlLib.Node, ByRef strItems as string)
   Dim nd as Node
   if not ( _item is nothing ) then
       strItems = strItems & _item.Text & vbCrLf
       
       set nd = _item.Child
       while not (nd  is Nothing)
           FetchNodesRecursive nd, strItems
           set nd = nd.Next
       wend
   end if    
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
   Dim colItems As String

   if not (Node is Nothing) Then
       FetchNodesRecursive Node, colItems
       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

0
ArkCommented:
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.
0
gowflowAuthor Commented:
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
0
ArkCommented:
Hi
I quoted your question to show you need direct children (not nested with reccursion) only.
'Returns comma-separated string with all direct children of given node
Private Function GetDirectChildren(ndParent As Node) As String
   Dim colItems As String
   Dim nd As Node
   Set nd = ndParent.Child
   Do While Not nd Is Nothing
      If colItems <> "" Then colItems = colItems & ","
      colItems = colItems & nd.Text
      Set nd = nd.Next
   Loop
   GetDirectChildren = colItems
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gowflowAuthor Commented:
@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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.