Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a TreeView to display property pages or option pages in Visual Basic .NET
DescriptionThis example shows how to use a TreeView to display property pages or option pages in Visual Basic .NET.
KeywordsTreeView, options, properties, property pages, option pages, Registry, save settings
CategoriesControls, Software Engineering
 
If you open Visual Studio's Tools menu and select the Options command, you'll see an enormous set of options that you can set. The TreeView on the left groups the option pages. You dig through the TreeView to find an option category and a page within the category, and then the area on the right displays that page's options.

This provides a useful alternative to the TabControl.

If you open Visual Studio's Project menu and select Properties at the bottom, you can see an example tab-based property pages. Generally this style is easier to use because it lets you see all of the categories on the tabs all at once. The TreeView style is most appropriate when you have a huge number of pages so you can't practically put them all in separate tabs.

Building a TreeView is easy enough. The tougher questions are:

  1. How do you associate the nodes in the tree with the property pages?
  2. How do you build the property pages at design time?
  3. How do you display the selected property page at run time?
  4. How do you save and restore the settings on the property pages?

1. How do you associate the nodes in the tree with the property pages?

Edit a TreeView control at design time to build the categories and sub-categories that you want. Set each node's Tag property to the index of the property page that you want it to display.

2. How do you build the property pages at design time?

This example uses a TabControl to hold the property pages because it is easy to use and lets you add controls to its pages at design time. You cannot simply have the TreeView select the appropriate tab, however, because the TabControl displays tabs that you probably don't want. Even if you make the tabs owner-drawn and give them the smallest size possible (1x1 pixel), you see an annoying little bump where the tabs would be.

To avoid that, the example moves the contents of the tabs out of the TabControls onto the form.

Put a Panel control inside each tab and put all of the tab's other controls inside the Panel. When the program's main form loads, the following code moves the Panels onto the form.

 
' Move the Panels out of the TabControl.
tabControl1.Visible = False
For Each page As TabPage In tabControl1.TabPages
    ' Add the Panel to the list.
    Dim the_panel As Panel = DirectCast(page.Controls(0), _
        Panel)
    Panels.Add(the_panel)

    ' Reparent and move the Panel.
    the_panel.Parent = tabControl1.Parent
    the_panel.Location = tabControl1.Location
    the_panel.Visible = False
Next page
 

3. How do you display the selected property page at run time?

When the user selects a TreeView node, the following code executes. The AfterSelect event handler converts the node's Tag property into an integer and passes it to the DisplayPanel function.

DisplayPanel hides the previously visible panel and displays the one with the indicated index.

 
' Display the appropriate Panel.
Private Sub treeView1_AfterSelect(ByVal sender As _
    System.Object, ByVal e As _
    System.Windows.Forms.TreeViewEventArgs) Handles _
    treeView1.AfterSelect
    Dim index As Integer = _
        Integer.Parse(e.Node.Tag.ToString())
    DisplayPanel(index)
End Sub

' Display the appropriate Panel.
Private Sub DisplayPanel(ByVal index As Integer)
    If (Panels.Count < 1) Then Return

    ' If this is the same Panel, do nothing.
    If (VisiblePanel Is Panels(index)) Then Return

    ' Hide the previously visible Panel.
    If (VisiblePanel IsNot Nothing) Then _
        VisiblePanel.Visible = False

    ' Display the appropriate Panel.
    Panels(index).Visible = True
    VisiblePanel = Panels(index)
End Sub
 

4. How do you save and restore the settings on the property pages?

The form's Load and FormClosing event handlers call the RegistryTools.LoadAllSettings and RegistryTools.SaveAllSettings functions described in the HowTo Easily save and restore all of a form's settings and the values of its controls in the Registry in Visual Basic .NET. These functions let the program easilyt save and restore all of the values on every option page quickly and easily.

Download the program to see additional details. Note that the program doesn't do anything with the settings. It just saves and restores them.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated