TreeView selection event : TreeView « GUI Windows Forms « C# / CSharp Tutorial

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. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » GUI Windows Forms » TreeView 
22. 31. 1. 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);
  }
}
22. 31. TreeView
22. 31. 1. TreeView selection event
22. 31. 2. Use TreeView to display Directories
22. 31. 3. TreeView: Add NodesTreeView: Add Nodes
22. 31. 4. Tree node foreground and background color, tooltipsTree node foreground and background color, tooltips
w_w_w_._ja__v_a__2_s___.__c_o_m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.