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 createSub 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    NextEnd Sub

You can use the above routine as follows:

TreeView1.Nodes.Add , , , "Root"' max 3 levels, max 5 nodes for each levelAddRandomNodes TreeView1, TreeView1.Nodes(1), 5, 3

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.