Simple Treeview : TreeView « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » TreeView 
23.31.1.Simple Treeview
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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(88);
      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(272232);
      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(8256);
      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(96256);
      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(513);
      this.ClientSize = new System.Drawing.Size(292301);
      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 == tnreturn;
      tn.ExpandAll();
    }

    private void OnCollapseNode(object sender, System.EventArgs e)
    {
      TreeNode tn = tvSimple.SelectedNode;
      if (null == tnreturn;
      tn.Collapse();
    }
  }
23.31.TreeView
23.31.1.Simple Treeview
23.31.2.Use TreeView.Nodes.AddRange to add nodes
23.31.3.TreeView selection event
23.31.4.Use TreeView to display Directories
23.31.5.TreeView: Add NodesTreeView: Add Nodes
23.31.6.Tree node foreground and background color, tooltipsTree node foreground and background color, tooltips
23.31.7.Directory TreeView
23.31.8.Hierarchical Tree View for displaying database table
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.