Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

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.

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Intermediate
Jul 31, 1999



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
End Sub
You can use the above routine as follows:

TreeView1.Nodes.Add , , , "Root"
' max 3 levels, max 5 nodes for each level
AddRandomNodes TreeView1, TreeView1.Nodes(1), 5, 3
Francesco Balena
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