TreeView selection event : 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.3.TreeView selection event
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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"00new TreeNode[] {new TreeNode("Cars"00new TreeNode[] {new TreeNode("Item1"00),new TreeNode("Item2"00)}),new TreeNode("Trucks"00new TreeNode[] {
        new TreeNode("Pickups"),new TreeNode("Utility vans")})})});
    this.treeView1.Size = new System.Drawing.Size(192293);
    this.treeView1.TabIndex = 0;
    this.treeView1.AfterSelect += new TreeViewEventHandler(this.treeView1_AfterSelect);
    // 
    // splitter1
    // 
    this.splitter1.Location = new System.Drawing.Point(1920);
    this.splitter1.Name = "splitter1";
    this.splitter1.Size = new System.Drawing.Size(3293);
    this.splitter1.TabIndex = 1;
    this.splitter1.TabStop = false;
    // 
    // listView1
    // 
    this.listView1.Dock = DockStyle.Fill;
    this.listView1.Location = new System.Drawing.Point(1950);
    this.listView1.Name = "listView1";
    this.listView1.Size = new System.Drawing.Size(141293);
    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(513);
    this.ClientSize = new System.Drawing.Size(336293);
    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);
  }
}
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.