Csharp/CSharp Tutorial/GUI Windows Forms/TreeView

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Directory TreeView

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
class MyClass : Form
{
    public static void Main()
    {
        Application.Run(new MyClass());
    }
    public MyClass()
    {
        TreeView tree = new TreeView();
        tree.Parent = this;
        tree.Dock = DockStyle.Fill;
        TreeNode nodeDriveC = new TreeNode("C:\\");
        tree.Nodes.Add(nodeDriveC);
        AddDirectories(nodeDriveC);
    }
    void AddDirectories(TreeNode node)
    {
        string strPath = node.FullPath;
        DirectoryInfo dirinfo = new DirectoryInfo(strPath);
        DirectoryInfo[] adirinfo;
        adirinfo = dirinfo.GetDirectories();
        foreach (DirectoryInfo di in adirinfo)
        {
            TreeNode nodeDir = new TreeNode(di.Name);
            node.Nodes.Add(nodeDir);
            AddDirectories(nodeDir);
        }
    }
}

Hierarchical Tree View for displaying database table

using System.Data;
using System.Data.SqlClient;
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
  public class HierarchicalTreeView : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TreeView tree;
    public HierarchicalTreeView()
    {
      this.tree = new System.Windows.Forms.TreeView();
      this.SuspendLayout();
      // 
      this.tree.ImageIndex = -1;
      this.tree.Location = new System.Drawing.Point(8, 8);
      this.tree.Name = "tree";
      this.tree.SelectedImageIndex = -1;
      this.tree.Size = new System.Drawing.Size(276, 248);
      this.tree.TabIndex = 0;
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.tree});
      this.Text = "Hierarchical TreeView";
      this.Load += new System.EventHandler(this.HierarchicalTreeView_Load);
      this.ResumeLayout(false);
    }
    string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
    string categorySQL = "SELECT * FROM Categories";
    string productSQL = "SELECT * FROM Products";
    private void HierarchicalTreeView_Load(object sender, System.EventArgs e)
    {
      SqlConnection con = new SqlConnection(connectionString);
      SqlCommand com = new SqlCommand(categorySQL, con);
      SqlDataAdapter adapter = new SqlDataAdapter(com);
      DataSet ds = new DataSet();
    
      con.Open();
      adapter.Fill(ds, "Categories");
      adapter.SelectCommand.rumandText = productSQL;
      adapter.Fill(ds, "Products");
      con.Close();
      DataColumn parentCol = ds.Tables["Categories"].Columns["CategoryID"];
      DataColumn childCol = ds.Tables["Products"].Columns["CategoryID"];
      DataRelation relation = new DataRelation("Cat_Prod", parentCol, childCol);
      ds.Relations.Add(relation);
      foreach (DataRow parent in ds.Tables["Categories"].Rows)
      {
        TreeNode nodeParent = tree.Nodes.Add(parent["CategoryName"].ToString());
        foreach (DataRow child in parent.GetChildRows(relation))
        {
          nodeParent.Nodes.Add(child["ProductName"].ToString());
        }    
      }
    }
    public static void Main()
    {
      Application.Run(new HierarchicalTreeView());
    }
  }

Simple Treeview

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
  public class SimpleTvForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TreeView tvSimple;
    private System.Windows.Forms.Button cmdExpandAll;
    private System.Windows.Forms.Button cmdCollapseAll;
    private System.Windows.Forms.ContextMenu ctxmTvNodes;
    private System.Windows.Forms.MenuItem mnuExpandNode;
    private System.Windows.Forms.MenuItem mnuCollapseNode;
    public SimpleTvForm()
    {
      this.tvSimple = new System.Windows.Forms.TreeView();
      this.ctxmTvNodes = new System.Windows.Forms.ContextMenu();
      this.mnuExpandNode = new System.Windows.Forms.MenuItem();
      this.mnuCollapseNode = new System.Windows.Forms.MenuItem();
      this.cmdExpandAll = new System.Windows.Forms.Button();
      this.cmdCollapseAll = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // tvSimple
      // 
      this.tvSimple.ContextMenu = this.ctxmTvNodes;
      this.tvSimple.ImageIndex = -1;
      this.tvSimple.Location = new System.Drawing.Point(8, 8);
      this.tvSimple.Name = "tvSimple";
      this.tvSimple.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                                         new System.Windows.Forms.TreeNode("Continents", new System.Windows.Forms.TreeNode[] {
                                                                                     new System.Windows.Forms.TreeNode("Africa"),
                                                                                     new System.Windows.Forms.TreeNode("America"),
                                                                                     new System.Windows.Forms.TreeNode("Europe"),
                                                                                     new System.Windows.Forms.TreeNode("Asia"),
                                                                                     new System.Windows.Forms.TreeNode("Australia")}),
                                         new System.Windows.Forms.TreeNode("SUV\"s", new System.Windows.Forms.TreeNode[] {
                                                                                   new System.Windows.Forms.TreeNode("Hummer"),
                                                                                   new System.Windows.Forms.TreeNode("Pinzgauer")})});
      this.tvSimple.SelectedImageIndex = -1;
      this.tvSimple.Size = new System.Drawing.Size(272, 232);
      this.tvSimple.TabIndex = 0;
      this.tvSimple.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.AfterSelect);
      // 
      // ctxmTvNodes
      // 
      this.ctxmTvNodes.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.mnuExpandNode,
                                            this.mnuCollapseNode});
      // 
      // mnuExpandNode
      // 
      this.mnuExpandNode.Index = 0;
      this.mnuExpandNode.Text = "Expand node";
      this.mnuExpandNode.Click += new System.EventHandler(this.OnExpandNode);
      // 
      // mnuCollapseNode
      // 
      this.mnuCollapseNode.Index = 1;
      this.mnuCollapseNode.Text = "Collapse node";
      this.mnuCollapseNode.Click += new System.EventHandler(this.OnCollapseNode);
      // 
      // cmdExpandAll
      // 
      this.cmdExpandAll.Location = new System.Drawing.Point(8, 256);
      this.cmdExpandAll.Name = "cmdExpandAll";
      this.cmdExpandAll.TabIndex = 1;
      this.cmdExpandAll.Text = "expand all";
      this.cmdExpandAll.Click += new System.EventHandler(this.cmdExpandAll_Click);
      // 
      // cmdCollapseAll
      // 
      this.cmdCollapseAll.Location = new System.Drawing.Point(96, 256);
      this.cmdCollapseAll.Name = "cmdCollapseAll";
      this.cmdCollapseAll.TabIndex = 2;
      this.cmdCollapseAll.Text = "collapse all";
      this.cmdCollapseAll.Click += new System.EventHandler(this.cmdCollapseAll_Click);
      // 
      // SimpleTvForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 301);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.cmdCollapseAll,
                                      this.cmdExpandAll,
                                      this.tvSimple});
      this.Name = "SimpleTvForm";
      this.Text = "Simple";
      this.ResumeLayout(false);
    }
    static void Main() 
    {
      Application.Run(new SimpleTvForm());
    }
    private void AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    {
      // MessageBox.Show("Node " +  e.Node.FullPath + " was selected");
    }
    private void cmdExpandAll_Click(object sender, System.EventArgs e)
    {
      tvSimple.ExpandAll();
    }
    private void cmdCollapseAll_Click(object sender, System.EventArgs e)
    {
      tvSimple.CollapseAll();
    }
    private void OnExpandNode(object sender, System.EventArgs e)
    {
      TreeNode tn = tvSimple.SelectedNode;
      if (null == tn) return;
      tn.ExpandAll();
    }
    private void OnCollapseNode(object sender, System.EventArgs e)
    {
      TreeNode tn = tvSimple.SelectedNode;
      if (null == tn) return;
      tn.Collapse();
    }
  }

Tree node foreground and background color, tooltips

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        treeView1.Nodes.Clear();
        treeView1.ShowNodeToolTips = true;
        TreeNode evenNumbers = treeView1.Nodes.Add("Even", "Even Numbers", 0,1);
        evenNumbers.BackColor = Color.Blue;
        evenNumbers.ForeColor = Color.Yellow;
        evenNumbers.ToolTipText = "The even numbers";
        TreeNode oddNumbers =  treeView1.Nodes.Add("Odd", "Odd Numbers", 0,1);
        oddNumbers.BackColor = Color.Yellow;
        oddNumbers.ForeColor = Color.Blue;
        oddNumbers.ToolTipText = "The odd numbers";
        for (int i = 1; i < 50; i++)
        {
            if (i % 2 == 0)
            {
                evenNumbers.Nodes.Add(i.ToString());
            } else {
                oddNumbers.Nodes.Add(i.ToString() );
            }
        }
    }
    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        TreeNode selectedNode = e.Node;
        if (selectedNode.Nodes.ContainsKey("1"))
        {
            Console.WriteLine("This node contains "1"");
        }
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.treeView1 = new System.Windows.Forms.TreeView();
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // treeView1
        // 
        this.treeView1.Location = new System.Drawing.Point(16, 50);
        this.treeView1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.treeView1.Name = "treeView1";
        this.treeView1.Size = new System.Drawing.Size(194, 149);
        this.treeView1.TabIndex = 0;
        this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(16, 15);
        this.button1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(100, 28);
        this.button1.TabIndex = 1;
        this.button1.Text = "Load";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(229, 213);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.treeView1);
        this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.TreeView treeView1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.ImageList imageList1;
}
public class TreeViewNodeBackgroundForegroundTooltip
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

TreeView: Add Nodes

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        treeView1.Nodes.Clear();
        TreeNode evenNumbers = treeView1.Nodes.Add("Even Numbers");
        TreeNode oddNumbers =  treeView1.Nodes.Add("Odd Numbers");
        for (int i = 1; i < 500; i++)
        {
            if (i % 2 == 0)
            {
                evenNumbers.Nodes.Add(i.ToString());
            } else {
                oddNumbers.Nodes.Add(i.ToString() );
            }
        }
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.treeView1 = new System.Windows.Forms.TreeView();
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // treeView1
        // 
        this.treeView1.Location = new System.Drawing.Point(16, 50);
        this.treeView1.Margin = new System.Windows.Forms.Padding(4);
        this.treeView1.Name = "treeView1";
        this.treeView1.Size = new System.Drawing.Size(286, 313);
        this.treeView1.TabIndex = 0;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(16, 15);
        this.button1.Margin = new System.Windows.Forms.Padding(4);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(100, 28);
        this.button1.TabIndex = 1;
        this.button1.Text = "Load";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(316, 390);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.treeView1);
        this.Margin = new System.Windows.Forms.Padding(4);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.TreeView treeView1;
    private System.Windows.Forms.Button button1;
}
public class TreeViewAddItems
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

TreeView selection event

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : Form
{
  private TreeView treeView1;
  private Splitter splitter1;
  private ListView listView1;
  public Form1()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
    this.treeView1 = new TreeView();
    this.splitter1 = new Splitter();
    this.listView1 = new ListView();
    this.SuspendLayout();
    this.treeView1.Dock = DockStyle.Left;
    this.treeView1.Name = "treeView1";
    this.treeView1.Nodes.AddRange(new TreeNode[] {new TreeNode("Vehicles", 0, 0, new TreeNode[] {new TreeNode("Cars", 0, 0, new TreeNode[] {new TreeNode("Item1", 0, 0),new TreeNode("Item2", 0, 0)}),new TreeNode("Trucks", 0, 0, new TreeNode[] {
        new TreeNode("Pickups"),new TreeNode("Utility vans")})})});
    this.treeView1.Size = new System.Drawing.Size(192, 293);
    this.treeView1.TabIndex = 0;
    this.treeView1.AfterSelect += new TreeViewEventHandler(this.treeView1_AfterSelect);
    // 
    // splitter1
    // 
    this.splitter1.Location = new System.Drawing.Point(192, 0);
    this.splitter1.Name = "splitter1";
    this.splitter1.Size = new System.Drawing.Size(3, 293);
    this.splitter1.TabIndex = 1;
    this.splitter1.TabStop = false;
    // 
    // listView1
    // 
    this.listView1.Dock = DockStyle.Fill;
    this.listView1.Location = new System.Drawing.Point(195, 0);
    this.listView1.Name = "listView1";
    this.listView1.Size = new System.Drawing.Size(141, 293);
    this.listView1.TabIndex = 2;
    this.listView1.View = View.List;
    this.listView1.ItemActivate += new System.EventHandler(this.listView1_ItemActivate);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(336, 293);
    this.Controls.AddRange(new Control[] {
                                             this.listView1,
                                             this.splitter1,
                                             this.treeView1});
    this.Name = "Form1";
    this.Text = "Vehicle Hierarchy";
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new Form1());
  }
  private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
  {
     switch (e.Node.Text)
     {
        case "Item1":
           listView1.Clear();
           listView1.Items.Add("A",3);
           listView1.Items.Add("B", 4);
           listView1.Items.Add("C", 6);
           break;
           
        case "Item2":
           listView1.Clear();
           listView1.Items.Add("D", 1);
           listView1.Items.Add("E", 2);
           listView1.Items.Add("F", 5);
           break;
     }
  }
  
  private void listView1_ItemActivate(object sender, System.EventArgs e) {
   String strItem = listView1.FocusedItem.Text;
   MessageBox.Show(strItem);
  }
}

Use TreeView to display Directories

/* Quote from 
Programming .NET Windows Applications
By Jesse Liberty, Dan Hurwitz
First Edition October 2003 
Pages: 1246 (More details)
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;        // necessary for Directory info
public class TreeViewDirectories : Form
{
  TreeView tvw;
  CheckBox cb;
  Button btnSelected;
  Button btnExpand;
  Button btnExpandAll;
  Button btnCollapse;
  Button btnCollapseAll;
  Button btnToggle;
  public TreeViewDirectories()
  {
    Size = new Size(400,600);
    ImageList imgList = new ImageList();
    Image img;
    String[] arFiles = {"1.ico","2.ico","3.ico",".ico"};

    for (int i = 0; i < arFiles.Length; i++)
    {
      img = Image.FromFile(arFiles[i]);
      imgList.Images.Add(img);
    }
    tvw = new TreeView();
    tvw.Parent = this;
    tvw.Location = new Point(10,10);
    tvw.Size = new Size(ClientSize.Width - 20, Height - 200);
    tvw.Anchor = AnchorStyles.Top | AnchorStyles.Left | 
          AnchorStyles.Right | AnchorStyles.Bottom;
    tvw.BackColor = Color.Moccasin;
    tvw.ForeColor = Color.DarkRed;
    tvw.BorderStyle = BorderStyle.Fixed3D;
    tvw.FullRowSelect = false;    
    tvw.ShowLines = true;      
    tvw.ShowPlusMinus = true;    
    tvw.Scrollable = true;      
    tvw.HideSelection = false;  
    tvw.HotTracking = true;  
    tvw.ImageList = imgList;
    tvw.ImageIndex = 1;
    tvw.SelectedImageIndex = 2;
    tvw.Indent = 35;
    tvw.Font = new Font("Times New Roman", 20f);
    tvw.ItemHeight = tvw.Font.Height * 2;
    tvw.BeforeExpand += new TreeViewCancelEventHandler(tvw_BeforeExpand);
    cb = new CheckBox();
    cb.Parent = this;
    cb.Location = new Point((Width - cb.Width) * 2 / 10, tvw.Bottom + 25);
    cb.Text = "Show Files";
    cb.Anchor = AnchorStyles.Bottom;
    cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
    btnSelected = new Button();
    btnSelected.Parent = this;
    btnSelected.Text = "&SelectedNode";
    int xSize = ((int)(Font.Height * .75) * btnSelected.Text.Length);
    int ySize = Font.Height + 10;
    btnSelected.Size = new Size(xSize, ySize);
    btnSelected.Location = new Point(cb.Left, cb.Bottom + ySize);
    btnSelected.Anchor = AnchorStyles.Bottom;
    btnSelected.Click += new EventHandler(btnSelected_Click);
    btnToggle = new Button();
    btnToggle.Parent = this;
    btnToggle.Location = new Point((Width - cb.Width) * 7 / 10,
                                      cb.Top);
    btnToggle.Text = "&Toggle";
    btnToggle.Size = new Size(btnSelected.Width, btnSelected.Height);
    btnToggle.Anchor = AnchorStyles.Bottom;
    btnToggle.Click += new EventHandler(btnToggle_Click);
    btnExpand = new Button();
    btnExpand.Parent = this;
    btnExpand.Location = new Point(btnToggle.Left, btnToggle.Bottom);
    btnExpand.Text = "&Expand";
    btnExpand.Size = new Size(btnSelected.Width, btnSelected.Height);
    btnExpand.Anchor = AnchorStyles.Bottom;
    btnExpand.Click += new EventHandler(btnExpand_Click);
    btnExpandAll = new Button();
    btnExpandAll.Parent = this;
    btnExpandAll.Location = new Point(btnExpand.Left, btnExpand.Bottom);
    btnExpandAll.Text = "Expand &All";
    btnExpandAll.Size = new Size(btnSelected.Width, btnSelected.Height);
    btnExpandAll.Anchor = AnchorStyles.Bottom;
    btnExpandAll.Click += new EventHandler(btnExpandAll_Click);
    btnCollapse = new Button();
    btnCollapse.Parent = this;
    btnCollapse.Location = new Point(btnExpandAll.Left, btnExpandAll.Bottom);
    btnCollapse.Text = "&Collapse";
    btnCollapse.Size = new Size(btnSelected.Width, btnSelected.Height);
    btnCollapse.Anchor = AnchorStyles.Bottom;
    btnCollapse.Click += new EventHandler(btnCollapse_Click);
    btnCollapseAll = new Button();
    btnCollapseAll.Parent = this;
    btnCollapseAll.Location = new Point(btnCollapse.Left, btnCollapse.Bottom);
    btnCollapseAll.Text = "Colla&pse All";
    btnCollapseAll.Size = new Size(btnSelected.Width, btnSelected.Height);
    btnCollapseAll.Anchor = AnchorStyles.Bottom;
    btnCollapseAll.Click += new EventHandler(btnCollapseAll_Click);
    FillDirectoryTree();
  }
  static void Main() 
  {
    Application.Run(new TreeViewDirectories());
  }
  private void FillDirectoryTree()
  {
    tvw.BeginUpdate();
    tvw.Nodes.Clear();
    string[] strDrives = Environment.GetLogicalDrives();
        foreach (string rootDirectoryName in strDrives)
       {
      try 
      {
        Directory.GetDirectories(rootDirectoryName);
        TreeNode ndRoot = new TreeNode(rootDirectoryName);
        tvw.Nodes.Add(ndRoot);
        if (ndRoot.Index % 2 == 0)
        {
          ndRoot.BackColor = Color.LightYellow;
          ndRoot.ForeColor = Color.Green;
        }
        GetSubDirectoryNodes(ndRoot, cb.Checked);
      }
      catch  (System.IO.IOException)
      {
            }
      catch  (Exception e)
      {
        MessageBox.Show(e.Message);
            }
    }
     
      tvw.EndUpdate();
     
  }
  private void GetSubDirectoryNodes(TreeNode parentNode, bool getFileNames)
  {
    DirectoryInfo di = new DirectoryInfo(parentNode.FullPath);
    if ((di.Attributes & FileAttributes.Directory) == 0)
    {
      return;
    }
    parentNode.Nodes.Clear();
    string[] arSubs = Directory.GetDirectories(parentNode.FullPath);
    foreach (string subDir in arSubs)
    {
          DirectoryInfo dirInfo = new DirectoryInfo(subDir);
            if ((dirInfo.Attributes & FileAttributes.Hidden)!= 0)
            {
               continue;
            }
      TreeNode subNode = new TreeNode(dirInfo.Name);
      parentNode.Nodes.Add(subNode);
        
      //  Set colors based on Index property.
      if (subNode.Index % 2 == 0)
        subNode.BackColor = Color.LightPink;
    }
    if (getFileNames)
    {
            //  Get any files for this node.
          string[] files = Directory.GetFiles(parentNode.FullPath);
            // After placing the nodes, 
            // now place the files in that subdirectory.
            foreach (string str in files)
            {
        FileInfo fi = new FileInfo(str);
        TreeNode fileNode = new TreeNode(fi.Name);
        parentNode.Nodes.Add(fileNode);
        //  Set the icon
        fileNode.ImageIndex = 0;
        fileNode.SelectedImageIndex = 3;
        //  Set colors based on Index property.
        if (fileNode.Index % 2 == 0)
          fileNode.BackColor = Color.LightGreen;
            }
    }
  }  // close GetSubDirectoryNodes

  private void cb_CheckedChanged(object sender, EventArgs e)
  {
    FillDirectoryTree();
  }
  private void tvw_BeforeExpand(object sender, 
                TreeViewCancelEventArgs e)
  {
    tvw.BeginUpdate();
    foreach (TreeNode tn in e.Node.Nodes)
    {
      GetSubDirectoryNodes(tn, cb.Checked);
    }
    tvw.EndUpdate();      
  }    
  private void btnSelected_Click(object sender, EventArgs e)
  {
    MessageBox.Show(tvw.SelectedNode.ToString() + "\n" +
        "FullPath:\t" + tvw.SelectedNode.FullPath.ToString() + "\n" +
           "Index:\t" + tvw.SelectedNode.Index.ToString());
  }
  private void btnExpand_Click(object sender, EventArgs e)
  {
    tvw.SelectedNode.Expand();
  }
  private void btnExpandAll_Click(object sender, EventArgs e)
  {
    tvw.SelectedNode.ExpandAll();
  }
  private void btnCollapse_Click(object sender, EventArgs e)
  {
    tvw.SelectedNode.Collapse();
  }
  private void btnCollapseAll_Click(object sender, EventArgs e)
  {
    tvw.CollapseAll();
  }
  private void btnToggle_Click(object sender, EventArgs e)
  {
    tvw.SelectedNode.Toggle();
  }
}