Use a TreeView to display property pages or option pages in C#
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:
- How do you associate the nodes in the tree with the property pages?
- How do you build the property pages at design time?
- How do you display the selected property page at run time?
- How do you save and restore the settings on the property pages?
// Move the Panels out of the TabControl.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.
tabControl1.Visible = false;
foreach (TabPage page in tabControl1.TabPages)
{
// Add the Panel to the list.
Panel panel = page.Controls[0] as Panel;
Panels.Add(panel);
// Reparent and move the Panel.
panel.Parent = tabControl1.Parent;
panel.Location = tabControl1.Location;
panel.Visible = false;
}
// Display the appropriate Panel.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 article Easily save and restore all of a form's settings and the values of its controls in the Registry in C#. These functions let the program easily 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.
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
int index = int.Parse(e.Node.Tag.ToString());
DisplayPanel(index);
}
// Display the appropriate Panel.
private void DisplayPanel(int index)
{
if (Panels.Count < 1) return;
// If this is the same Panel, do nothing.
if (VisiblePanel == Panels[index]) return;
// Hide the previously visible Panel.
if (VisiblePanel != null) VisiblePanel.Visible = false;
// Display the appropriate Panel.
Panels[index].Visible = true;
VisiblePanel = Panels[index];
}


Thanks for this article. It was just what I needed.
[Note: Sometimes the web site messes up comments that contain code. I tried to fix this but if I got it wrong, Todd, let me know and I'll fix it. Rod]
Reply to this