TreeViews are a wonderful tool in .NET and we use them all over the place...however, though there are similarities between each use, the TreeView is always a little bit different, thus making it a little more difficult to have a hard and fast standard.Here are some of the standards that we follow though. The best advice is to create recursive methods that build each of the nodes.
Private Sub LoadTree()
'-- Establish Locals
Dim loNode As TreeNode
'-- Clear the tree
MyTreeView.Nodes.Clear()
'-- Cycle through all of the parent records
If MyBO.MoveFirst()
Do
'-- Create the new root node
loNode = New TreeNode(MyBO.MyTitleField)
'-- Load any children nodes
LoadChildNodes(MyBO, loNode)
Loop While MyBO.MoveNext()
End If
End SUb
Private Sub LoadChildNodes(Byval ParentBO As BusinessLayer, Byval ParentNode As TreeNode)
'-- Establish Locals
Dim loNode As TreeNode
Dim loChildBO As New MyChildBO
'-- Load the child records
loChildBO.FillByParentPrimaryKey(ParentBO.PrimaryKey)
'-- Cycle through the child nodes
If loChildBO.MoveFirst()
Do
'-- Create the node
loNode = New TreeNode(loChildBO.TitleField)
'-- Add to parent nodes
ParentNode.Nodes.Add(loNode)
'-- Check for child nodes
If loChildBO.HasChildRecords Then
LoadChildNodes(loChildBO, loNode)
End If
Loop While loChildBO.MoveNext()
End If
'-- Clean Up
loChildBO.Dispose()
End Sub
Also, we generally create a class and store it in the tag property of each node so that when it is clicked we know exactly what to do. You can see a simple example of this in the samples that were installed with the framework. I think it is called Explorer Form Sample.