With the Windows Forms http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx control, you can display a hierarchy of nodes to users, like the way files and folders are displayed in the left pane of the Windows Explorer feature of the Windows operating system. Each node in the tree view might contain other nodes, called child nodes. You can display parent nodes, or nodes that contain child nodes, as expanded or collapsed. You can also display a tree view with check boxes next to the nodes by setting the tree view's http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.checkboxes.aspx property to true. You can then programmatically select or clear nodes by setting the node's http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.checked.aspx property to true or false.
namespace TreeViewExample
{
/// <summary>
/// Summary description for TreeViewExample.
/// </summary>
public class TreeViewExample : System.Windows.Forms.Form
{
internal System.Windows.Forms.TreeView treeFood;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public TreeViewExample()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.treeFood = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// treeFood
//
this.treeFood.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.treeFood.ImageIndex = -1;
this.treeFood.Location = new System.Drawing.Point(4, 5);
this.treeFood.Name = "treeFood";
this.treeFood.SelectedImageIndex = -1;
this.treeFood.Size = new System.Drawing.Size(284, 256);
this.treeFood.TabIndex = 1;
this.treeFood.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeFood_AfterSelect);
//
// TreeViewExample
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.treeFood});
this.Name = "TreeViewExample";
this.Text = "TreeView Example";
this.Load += new System.EventHandler(this.TreeViewExample_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new TreeViewExample());
}
private void TreeViewExample_Load(object sender, System.EventArgs e)
{
TreeNode node;
node = treeFood.Nodes.Add("Fruits");
node.Nodes.Add("Apple");
node.Nodes.Add("Peach");
node = treeFood.Nodes.Add("Vegetables");
node.Nodes.Add("Tomato");
node.Nodes.Add("Eggplant");
}
private void treeFood_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if (e.Action == TreeViewAction.ByMouse)
{
MessageBox.Show(e.Node.FullPath);
}
}
}
}