Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Fill a TreeView control with random data

Total Hit ( 2157)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
' 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:

Click here to copy the following block
TreeView1.Nodes.Add , , , "Root"
' max 3 levels, max 5 nodes for each level
AddRandomNodes TreeView1, TreeView1.Nodes(1), 5, 3


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.