Visual Basic: Build a treeview control to show database records

Platform:Visual Basic
Task:Build a treeview control to show database records
Discussion:
The new VB .Net TreeView control is tantalizingly powerful for showing hierarchical data in a very intuitive and organized fashion. It is also infuriatingly hard to figure out, especially if you come at it after having used the VBA tree control in the past. You really need to change your thinking to make it work. In VBA I was used to thinking of the tree model as a single hierarchy of nodes that you referred to by traveling down the nodes. VB .Net is different in that you need to think of the tree model as a conglomeration of treenode objects that you refer to individually. For instance to add another grandson to a great grandfather in a vba tree model you might do this (pseudocode): GreatGrandfather.Grandfather.Father.Nodes().Add(Grandson) You refer to the node by cascading down the tree model until you get to the node to which you want to add a new node. VB is different. You don't need to know anything about the tree model. You only need to know which treenode object you want to add the new node to: Dim Grandson as New TreeNode() Father.Nodes().Add(Grandson) You don't need to know about the grandfather or the great grandfather nodes. The example below is a simple example showing a hypothetical mammal survey database application that shows a hierarchical tree of mammal survey types and survey dates. This form's treeview could be used to determine which forms and database records should be shown in the application.
Example:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'create a top-level root node to hold all the mammal survey nodes in the treeview control
        Dim Node_Root As New TreeNode()
        Node_Root.Text = "Mammal Surveys"
        Node_Root.Tag = "frmMammalSurveysMaster"

        'create a spring moose survey subnode
        Dim Node_SpringMooseSurveys As TreeNode
        Node_SpringMooseSurveys = New TreeNode
        Node_SpringMooseSurveys.Text = "Spring Moose Surveys" 'add node text
        Node_SpringMooseSurveys.Tag = "frmSpringMooseSurveysMaster" 'add node tag.  In this case I store the name of the form I want to call up when the node is clicked
        Node_Root.Nodes.Add(Node_SpringMooseSurveys) 'add this node to the root node

        Dim Node_SpringBearSurveys As TreeNode
        Node_SpringBearSurveys = New TreeNode
        Node_SpringBearSurveys.Text = "Spring Bear Surveys" 'add node text
        Node_SpringBearSurveys.Tag = "frmSpringBearSurveysMaster" 'add node tag.  In this case I store the name of the form I want to call up when the node is clicked
        Node_Root.Nodes.Add(Node_SpringBearSurveys) 'add this node to the root node

        Dim Node_SpringBearSurvey1 As TreeNode
        Node_SpringBearSurvey1 = New TreeNode
        Node_SpringBearSurvey1.Text = "Survey 2008-09-15" 'add node text
        Node_SpringBearSurvey1.Tag = "frmSpringBearSurveyDetail|SurveyID=145" 'add node tag.  the form to call and the id of the database record to show (these are pipe | separated values that I can parse later
        Node_SpringBearSurveys.Nodes.Add(Node_SpringBearSurvey1) 'add this node to the spring bear surveys node

        Dim Node_SpringBearSurvey2 As TreeNode
        Node_SpringBearSurvey2 = New TreeNode
        Node_SpringBearSurvey2.Text = "Survey 2008-05-27" 'add node text
        Node_SpringBearSurvey2.Tag = "frmSpringBearSurveyDetail|SurveyID=694" 'add node tag.  the form to call and the id of the database record to show (these are pipe | separated values that I can parse later
        Node_SpringBearSurveys.Nodes.Add(Node_SpringBearSurvey2) 'add this node to the spring bear surveys node

        treeNav.Nodes.Add(Node_Root)
    End Sub

    Private Sub treeNav_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeNav.AfterSelect
        'the form has a text box. when a node is clicked the text box should show the selected node's text and the tag value.  The text 
        'should provide the context for the node, the tag should contain the hidden 'stuff' your application needs to operate.  In this case the 
        'tag contains the correct form to show the user and the primary key value of the record to show, if needed
        txtOut.AppendText("THE NODE'S TEXT:" & treeNav.SelectedNode.Text & " THE NODE'S TAG: " & treeNav.SelectedNode.Tag & vbNewLine)
    End Sub
End Class
Back to Index