TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Language: VB4/32,VB5,VB6
Expertise: Intermediate
Jul 31, 1999

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# Fill a TreeView control with random data

Every now and then you need to fill a TreeView control with some random data, for example when you want to test a routine and you don't want to write a lot of code just for this secondary task. Here is a recursive routine that does the work for you:

``````
' MaxChildren is the max number of child Nodes at each level
' MaxLevel is the deepest nesting level you want to create

Sub AddRandomNodes(TV As TreeView, Node As Node, MaxChildren As Integer, _
MaxLevel As Integer)

Dim i As Integer
Dim child As Node

' Add a number of child Nodes less or equal to MaxChildren
For i = 1 To CInt(Rnd * MaxChildren)
Set child = TV.Nodes.Add(Node.index, tvwChild, , _
"Node #" & (TV.Nodes.Count + 1))
' for each child Node, if MaxLevel is greater than 0
' randomly add a set of child nodes
If CInt(Rnd * MaxLevel) Then
AddRandomNodes TV, child, MaxChildren, MaxLevel - 1
End If
Next
End Sub
``````
You can use the above routine as follows:

``````
' max 3 levels, max 5 nodes for each level
``````
Francesco Balena

 Submit a Tip Browse "Application Design" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date