Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Remove items if any added at design time
Me.C1TreeView1.Nodes.Clear()
'Add first two levels of nodes to create a basic structure of TreeView
' Scan every type in the assembly
For Each t As Type In Me.C1TreeView1.GetType().Assembly.GetTypes()
If t.IsPublic AndAlso Not t.IsSpecialName AndAlso Not t.IsAbstract Then
' Add node for this type
Dim node As C1TreeNode = New C1TreeNode()
node.SetValue(t.Name)
' Save information needed to populate the node
node.Tag = t
Me.C1TreeView1.Nodes.Add(node)
' Add subnodes for properties, events, and methods
node.Nodes.Add(CreateMemberNode("Properties", t, MemberTypes.Property))
node.Nodes.Add(CreateMemberNode("Events", t, MemberTypes.Event))
node.Nodes.Add(CreateMemberNode("Methods", t, MemberTypes.Method))
End If
Next
'Handle the Expanding event to add nodes on demand
AddHandler Me.C1TreeView1.Expanding, AddressOf Me.C1TreeView1_Expanding
End Sub
'Creating and adding a node to TreeView
Private Function CreateMemberNode(ByVal header As String, ByVal t As Type, ByVal memberTypes As MemberTypes) As C1TreeNode
' Create the node
Dim node As C1TreeNode = New C1TreeNode()
node.SetValue(header)
' Save information needed to populate the node
node.Tag = memberTypes
' Add a dummy node so this node can be expanded
node.Nodes.Add(New C1TreeNode())
node.Expanded = False
' Finish
Return node
End Function
Private Sub C1TreeView1_Expanding(sender As Object, e As C1TreeViewCancelEventArgs) Handles C1TreeView1.Expanding
' Get the node that fired the event
Dim node As C1TreeNode = TryCast(e.Node, C1TreeNode)
If node.Level <> 0 Then
' Remove dummy node
node.Nodes.Clear()
' Populate the node
Dim type As Type = CType(node.ParentCollection.Parent.Tag, Type)
Dim memberTypes As MemberTypes = node.Tag
Dim bf As BindingFlags = BindingFlags.Public Or BindingFlags.Instance
For Each mi As MemberInfo In type.GetMembers(bf)
If mi.MemberType = memberTypes Then
If Not mi.Name.StartsWith("get_") AndAlso Not mi.Name.StartsWith("set_") Then
Dim item As C1TreeNode = New C1TreeNode()
item.SetValue(mi.Name)
node.Nodes.Add(item)
End If
End If
Next
End If
End Sub