Introduction to Creating Tree View Nodes
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…
Visually Creating Nodes#
A tree view behaves as a standard control within a Windows environment. You can add one by selecting the TreeView icon in the Toolbox and placing it on a form, which performs the same action as instantiating a TreeView class with the new keyword and using Controls.Add() to register it. For design-time node creation, Visual Studio offers a specialized dialog called the TreeNode Editor, accessible via the ellipsis button in the Nodes row of the Properties window once a tree view is selected.
The primary characteristic of a node is the text it displays. At design time and in the TreeNode Editor, to create a node, you can more readily click the Add Root button. When you do this, a node with a default but incremental name is created. To edit a node's name, first select it in the Select Node To Edit list, then, on the right side, click Name and enter the string you wish while keeping the original behavior intact.
Programmatically Creating Nodes#
The Nodes property serves as the storage for all branches in a tree view, managed by the TreeNodeCollection class. This class follows the IList, ICollection, and IEnumerable interfaces to represent a collection of TreeNode objects. To add a new node programmatically at runtime, you use the TreeNodeCollection.Add() method, which provides two different overloads. The syntax for one of these versions is as follows:
public virtual TreeNode Add(string text);This specific method accepts a string parameter representing the label for the new branch. It is frequently used for establishing a root node in the hierarchy. Here is a sample call:
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;
tvwCountries.Nodes.Add("World");
Controls.Add(tvwCountries);
}This would produce:
Practical Learning: Creating the Root Node
- Double-click an unoccupied area on the body the form to generate its Load event
- To create the first node of the tree, implement the event as follows: private void Central_Load(object sender, EventArgs e) { TreeNode nodRoot = tvwAutoParts.Nodes.Add("College Park Auto-Parts"); for (int i = DateTime.Today.Year + 1; i > 1960; i--) nodRoot.Nodes.Add(i.ToString()); }
- Execute the application to test the form
- Close the form and return to your programming environment
- On the main menu, click Project -> Add Class...
- Set the name to PartDescription and press Enter
- To create a class that can holds a structured item of a list, change the class as follows: using System; namespace CollegeParkAutoParts2 { public class PartDescription { // These members will be used to define a car part private long ID; private int yr; private string mk; private string mdl; private string cat; private string name; private decimal price; public PartDescription() { this.ID = 0; this.yr = 1960; this.mk = ""; this.mdl = ""; this.name = "Unknown"; this.price = 0.00M; } public PartDescription(long code, int year, string make, string model, string type, string desc, decimal UPrice) { this.ID = code; this.yr = year; this.mk = make; this.mdl = model; this.cat = type; this.name = desc; this.price = UPrice; } public long PartNumber { get { return ID; } set { ID = value; } } public int CarYear { get { return yr; } set { yr = value; } } public string Make { get { return mk; } set { mk = value; } } public string Model { get { return mdl; } set { mdl = value; } } public string Category { get { return cat; } set { cat = value; } } public string PartName { get { return name; } set { name = value; } } public decimal UnitPrice { get { return (price < 0) ? 0.00M : price; } set { price = value; } } public override string ToString() { return this.PartNumber + " " + this.CarYear.ToString() + " " + this.Make + " " + this.Model + " " + this.Category + " " + this.PartName + " " + this.UnitPrice; } } }
Previous Copyright © 2008-2009 C# Key Next

