Home

Introduction to Creating Tree View Nodes

 

Visually Creating 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 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:

To create the nodes of a tree view, Microsoft Visual Studio provides a convenient dialog box you can use at design time. To display it, after adding a tree view control to a form, you can click the ellipsis button of its Nodes field in the Properties window. This would open the TreeNode Editor:

Tree Node Editor

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 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.

Programmatically Creating Nodes

The branches of a tree view are stored in a property called Nodes. The Nodes property is an object based on the TreeNodeCollection class. The TreeNodeCollection class implements the IList, the ICollection, and the IEnumerable interfaces.

As its name indicates, the Nodes property carries all of the branches of a tree view. This means that the Nodes property in fact represents a collection. Each member of this collection is called a node and it is an object based on the TreeNode class.

At run time, to create a new node, call the TreeNodeCollection.Add() method which is overloaded with two versions. One of the versions of this method uses the following syntax:

public virtual TreeNode Add(string text);

This method takes as argument the string that the branch will display. This method is also the prime candidate to create a root node. Here is an example of calling it:

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:

Countries Statistics

Practical LearningPractical Learning: Creating the Root Node

  1. Double-click an unoccupied area on the body the form to generate its Load event
  2. 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());
    }
  3. Execute the application to test the form
     
    College Park Auto-Parts
  4. Close the form and return to your programming environment
  5. On the main menu, click Project -> Add Class...
  6. Set the name to PartDescription and press Enter
  7. 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 © 2007-2010 FunctionX, Inc. Next