ListBox Events: SelectedIndexChanged, SelectedValueChanged, DataSourceChanged, DisplayMemberChanged, ValueMemberChanged : ListBox « 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
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
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
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » GUI Windows Forms » ListBox 
22. 27. 8. ListBox Events: SelectedIndexChanged, SelectedValueChanged, DataSourceChanged, DisplayMemberChanged, ValueMemberChanged
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class Employee

  public string ID;
  public string Name ;

  public  Employee(string strName, string strID)
  {
     this.ID = strID;
     this.Name = strName;
  }

  public override string ToString()
  {
     return this.ID + " : " this.Name;
  }


public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.RadioButton rbAuthors;
  private System.Windows.Forms.RadioButton rbEmployees;
  private System.Windows.Forms.ListBox lb;

  private DataTable dataTable;
    private ArrayList Employees = new ArrayList();

  private System.ComponentModel.Container components = null;

  public Form1()
  {
    InitializeComponent();

    lb.Items.Add("A");
    lb.Items.Add("B");
    lb.Items.Add("C");
    lb.Items.Add("D");
    lb.Items.Add("E");

    lb.SelectedIndex = 0;

    // populate the arraylist for later use.
        Employees.Add(new Employee("A""1"));
        Employees.Add(new Employee("B""2"))
        Employees.Add(new Employee("C""3"));
        Employees.Add(new Employee("D""4"));
        Employees.Add(new Employee("E""5"));
        Employees.Add(new Employee("F""6"));
        Employees.Add(new Employee("G""7"));
  }

  protected override void Disposebool disposing )
  {
    ifdisposing )
    {
      if (components != null
      {
        components.Dispose();
      }
    }
    base.Disposedisposing );
  }

  private void InitializeComponent()
  {
    this.lb = new System.Windows.Forms.ListBox();
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.rbEmployees = new System.Windows.Forms.RadioButton();
    this.rbAuthors = new System.Windows.Forms.RadioButton();
    this.groupBox1.SuspendLayout();
    this.SuspendLayout();
    // 
    // lb
    // 
    this.lb.Location = new System.Drawing.Point(168);
    this.lb.Name = "lb";
    this.lb.Size = new System.Drawing.Size(232212);
    this.lb.TabIndex = 0;
    this.lb.DisplayMemberChanged += new System.EventHandler(this.lb_DisplayMemberChanged);
    this.lb.ValueMemberChanged += new System.EventHandler(this.lb_ValueMemberChanged);
    this.lb.DataSourceChanged += new System.EventHandler(this.lb_DataSourceChanged);
    this.lb.SelectedValueChanged += new System.EventHandler(this.lb_SelectedValueChanged);
    this.lb.SelectedIndexChanged += new System.EventHandler(this.lb_SelectedIndexChanged);
    // 
    // groupBox1
    // 
    this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                    this.rbEmployees,
                                                    this.rbAuthors});
    this.groupBox1.Location = new System.Drawing.Point(16240);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.TabIndex = 1;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "DataSource";
    // 
    // rbEmployees
    // 
    this.rbEmployees.Location = new System.Drawing.Point(2456);
    this.rbEmployees.Name = "rbEmployees";
    this.rbEmployees.TabIndex = 1;
    this.rbEmployees.Text = "Employee\'s";
    this.rbEmployees.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
    // 
    // rbAuthors
    // 
    this.rbAuthors.Checked = true;
    this.rbAuthors.Location = new System.Drawing.Point(2432);
    this.rbAuthors.Name = "rbAuthors";
    this.rbAuthors.TabIndex = 0;
    this.rbAuthors.TabStop = true;
    this.rbAuthors.Text = "Authors";
    this.rbAuthors.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
    this.ClientSize = new System.Drawing.Size(264389);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                             this.groupBox1,
                                             this.lb});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.groupBox1.ResumeLayout(false);
    this.ResumeLayout(false);

  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new Form1());
  }

  private void rb_CheckedChanged(object sender, System.EventArgs e)
  {
    lb.DataSource = Employees;
    lb.DisplayMember = "Name";
    lb.ValueMember = "ID";
  }

  private void lb_SelectedIndexChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.SelectedIndex.ToString()"\n" + lb.GetItemText(lb.SelectedItem),"lb_SelectedIndexChanged");    
  }

  private void lb_SelectedValueChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.GetItemText(lb.SelectedItem),"lb_SelectedValueChanged");    
  }

  private void lb_DataSourceChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.DataSource.ToString()"lb_DataSourceChanged");    
  }

  private void lb_DisplayMemberChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.DisplayMember.ToString()"lb_DisplayMemberChanged");    
  }

  private void lb_ValueMemberChanged(object sender, System.EventArgs e)
  {
    MessageBox.Show(lb.ValueMember.ToString()"lb_ValueMemberChanged");    
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
    this.lb.SelectedValueChanged += new System.EventHandler(this.lb_SelectedValueChanged);
    this.lb.SelectedIndexChanged += new System.EventHandler(this.lb_SelectedIndexChanged);
  }
}
22. 27. ListBox
22. 27. 1. Set the TopIndex property of the ListBox to ensure the most recently added items are visibleSet the TopIndex property of the ListBox to ensure the most recently added items are visible
22. 27. 2. Add Items to ListBoxAdd Items to ListBox
22. 27. 3. Add ContextMenu to ListBoxAdd ContextMenu to ListBox
22. 27. 4. ListBox Items AddListBox Items Add
22. 27. 5. ListBox Items Add a RangeListBox Items Add a Range
22. 27. 6. ListBox Selection ModeListBox Selection Mode
22. 27. 7. Use RadioButton to control ListBox selection modeUse RadioButton to control ListBox selection mode
22. 27. 8. ListBox Events: SelectedIndexChanged, SelectedValueChanged, DataSourceChanged, DisplayMemberChanged, ValueMemberChanged
22. 27. 9. Add text in the TextBox to the ListBox
w__ww.__j_a__va_2_s__.__co___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.