simple treeview question

Hi,
i need to print the content of a treee veiw as they are i mean print the father then its childred under it like:
1
  11
  12
    121
    122
    123
  13
2
 21
   211
   212
 22
 23
i need to print them on the printer but i dont want to use the indexs coz it will print them as levels i mean it will print the whole first level then the whole second level and so on  

thanx for help
Loay
loayAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Julian_KConnect With a Mentor Commented:
Oh, How STUPID I AM !!! :-((

The problem is in the brackets in line 4:

Private Sub PrintTreeView(ByVal Item As MSComctlLib.Node)
1:   Do Until (Item Is Nothing)
2:     Printer.Print Item.Text
3:     If Item.Children > 0 Then PrintTreeView (Item.Child)
4:     Set Item = Item.Next                   '^^^^^^^^^^^^
5:   Loop
   
End Sub

It must be like:
  if Item.Children > 0 then PrintTreeView Item.Child

I am very sorry for this stupid bug of mine...
0
 
trowaCommented:
Do a loop to compare each node in TreeView, if it's child then Add vbTab to the string to print.
0
 
loayAuthor Commented:
i know this but how can i write it you know i need aloop to check if it is a parent or not and if it a parent prints its child then go and print the second parent and its child but how to write this code
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Julian_KCommented:
Hello.
Here is a very simple answer to your problem, but it uses recursion. The recursion level depends on how many times an item has a child. I mean:
if there is a CHILD that has a CHILD that has a CHILD that has a CHILD - this is level 4 recursion.

The code that follows is usefull, if the recursion level will be not more than... let's say 100 ;-)


Private Sub PrintTreeView(ByVal Item As MSComctlLib.Node)
   
    Do Until (Item Is Nothing)
        Printer.Print Item.Text
        If Item.Children > 0 Then PrintTreeView(Item.Child)
        Set Item = Item.Next
    Loop
   
End Sub

I hope it will be useful

Julian.
0
 
loayAuthor Commented:
Hi Julian,
first thanx for trying help me ,, i tyoed ur code but the problem is when i wana call it i tried many things to send them as parameters but it was always say "type missmatch" so please can u tell me how can i call the procedure and send my tree to it

thanx alot
Loay
0
 
Julian_KCommented:
Hello, loay.
Sorry for this confusion.
The parameter you have to send to this procedure is an object of type "Node" - an item from the treeview. It must be the first sibling, if you want to print the whole tree. So, let's say you have a:
 TreeView control called "TreeView1"
 Command Button called "Command1"

your code would look like this:
-------------------------------

Private sub Command1_Click()
   Call PrintTreeView(TreeView1.Nodes(1))
End Sub

Try it and call back. I'l be online again tomorrow morning :-)
0
 
loayAuthor Commented:
hi again thanx again for ur support but this time it gives me an error on the
 if Item.Children > 0 Then PrintTreeView(Item.Child)
and it says "type missmatch" ,, so what should i do to solve this problem

thanx alot again
 
       
0
 
i_jester69Commented:
Just get rid of the parentheses around the item.child parameter or use the 'Call' methodology:

Private Sub PrintTreeView(ByVal Item As MSComctlLib.Node)
   
   Do Until (Item Is Nothing)
       Printer.Print Item.Text
       If Item.Children > 0 Then PrintTreeView Item.Child
       Set Item = Item.Next
   Loop
   
End Sub

Private Sub PrintTreeView(ByVal Item As MSComctlLib.Node)
   
   Do Until (Item Is Nothing)
       Printer.Print Item.Text
       If Item.Children > 0 Then Call PrintTreeView(Item.Child)
       Set Item = Item.Next
   Loop
   
End Sub
0
 
loayAuthor Commented:
ok thanx alot it is working fine now but please can u explain to me why when we use the brackets it gives that error.. and what is the defferent between calling with () and without them

any way if u can answer me it is cool i will wait for tom. and i wil send u ur points if u can explain it is good if cant no problem u helped me alot

loay


and thanx alot for i_jester69  u helped me alot.
0
 
Julian_KCommented:
When You use brackets (I mean brackets that are around an expression, not the ones that are needed to be put around parameters), VB decides, that this is an expresion and tries to evaluate it internaly, so When you pass object into brackets, It evaluates it as a variant of subtype object. I think that is what happens. Really Stupid :-)))

0
 
loayAuthor Commented:
thanx for clearation and i hope to be friends ... thanx alot
Loay
0
All Courses

From novice to tech pro — start learning today.