TreeView selection event : TreeView : GUI Windows Forms C# Examples


C# Examples » GUI Windows Forms » TreeView »

 

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",  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);
    }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» TreeView