Windows Control: The Tree View
A tree view is a control that resembles an upside down tree and displays a hierarchical list of items. Like a normal tree, a tree view starts in the top…
Introduction#
Functioning like an inverted tree, the tree view control provides a way to show a hierarchical list of objects. It begins with a root element at the top, which then divides into branches and leaves in a real tree, though in a software application, every branch is simply called a node.
In real world, a leaf cannot have a branch as its child, only a branch can more readily have another branch as its child and a branch can have a leaf as a child. In an application, a node (any node) can have a node as a child. Like a real world tree, the branches or nodes of a tree view use a type of relationship so that they are not completely independent. For example, a tree view can be based on a list that has a parent item and other child items that depend on that parent. In real world, if you cut a branch, the branches and leaves attached to it also disappear. This scenario is also valid for a tree view while keeping the original behavior intact.
Although natural trees usually have just one root, it is possible for a tree view in a Windows application to feature multiple roots.
Tree View Creation#
In a Windows application a tree view is primarily a control like any other. To use it in your application, you can click the TreeView button in the Toolbox and click a form or other control in your application. This is equivalent to programmatically declaring a variable of type TreeView, using the new operator to instantiate it and adding it to its container's list of controls through a call to the Controls.Add() method. Here is an example:
using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : System.Windows.Forms.Form
{
TreeView tvwCountries;
public Exercise()
{
InitializeComponent();
}
private void InitializeComponent()
{
Text = "Countries Statistics";
Size = new Size(242, 280);
tvwCountries = new TreeView();
tvwCountries.Location = new Point(12, 12);
tvwCountries.Width = 210;
tvwCountries.Height = 230;
Controls.Add(tvwCountries);
}
}
public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}This would produce:
Simply declaring a TreeView variable places an empty rectangular container into your interface. Your subsequent step will likely involve populating this control with one or several branches. Hands-on Tutorial: An Introduction to the Tree View Control.
- Start Microsoft Visual C#
- Create a new Windows Application named CollegeParkAutoParts2
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Central.cs and press Enter
- Design the form as follows: Control Text Name Other Properties Label College Park Auto-Parts Font: Times New Roman, 20.25pt, style=Bold Panel Height: 2 TreeView tvwAutoParts GroupBox ListView lvwAutoParts View: Details Columns (Name) Text TextAlign Width colPartNumber Part # colPartName Part Name 300 colUnitPrice Unit Price Right 80 GroupBox Selected Parts Label Part # Label Part Name Label Unit Price Label Qty Label Sub Total TextBox txtPartNumber1 TextBox txtPartName1 TextBox 0.00 txtUnitPrice1 TextAlign: Right TextBox 0 txtQuantity1 TextAlign: Right TextBox 0.00 txtSubTotal1 TextAlign: Right Button Remove btnRemove1 TextBox txtPartNumber2 TextBox txtPartName2 TextBox 0.00 txtUnitPrice2 TextAlign: Right TextBox 0 txtQuantity2 TextAlign: Right TextBox 0.00 txtSubTotal2 TextAlign: Right Button Remove btnRemove2 TextBox txtPartNumber3 TextBox txtPartName3 TextBox 0.00 txtUnitPrice3 TextAlign: Right TextBox 0 txtQuantity3 TextAlign: Right TextBox 0.00 txtSubTotal3 TextAlign: Right Button Remove btnRemove3 TextBox txtPartNumber4 TextBox txtPartName4 TextBox 0.00 txtUnitPrice4 TextAlign: Right TextBox 0 txtQuantity4 TextAlign: Right TextBox 0.00 txtSubTotal4 TextAlign: Right Button Remove btnRemove4 TextBox txtPartNumber5 TextBox txtPartName5 TextBox 0.00 txtUnitPrice5 TextAlign: Right TextBox 0 txtQuantity5 TextAlign: Right TextBox 0.00 txtSubTotal5 TextAlign: Right Button Remove btnRemove5 TextBox txtPartNumber6 TextBox txtPartName6 TextBox 0.00 txtUnitPrice6 TextAlign: Right TextBox 0 txtQuantity6 TextAlign: Right TextBox 0.00 txtSubTotal6 TextAlign: Right Button Remove btnRemove6 GroupBox Order Summary Button New Auto-Part... btnNewAutoPart Label Total Order: TextBox 0.00 txtTotalOrder Button Close btnClose
- Execute the application to test it
- Close the form and return to your programming environment
Home Copyright © 2008-2009 C# Key Next

