Csharp/C Sharp by API/System.Windows.Forms/ListBox

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

ListBox.BeginUpdate()

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class ListBoxItemAdd : Form{

 ListBox lb;
 public ListBoxItemAdd()
 {
   Size = new Size(300,400);
   lb = new ListBox();
   lb.Parent = this;
   lb.Location = new Point(10,10);
   lb.Size = new Size(ClientSize.Width - 20, Height - 200);
   lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
   lb.BorderStyle = BorderStyle.Fixed3D;
   lb.BeginUpdate();
   for (int i = 0; i < 5; i++)
   {
      lb.Items.Add(i);
   }      
   lb.Items.Add("12345");
   lb.Items.Add("67890");
   lb.EndUpdate();
 } 
 static void Main() 
 {
   Application.Run(new ListBoxItemAdd());
 }

}

 </source>


ListBox.ContextMenu

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class ListBoxContextMenu : System.Windows.Forms.Form {

 private System.Windows.Forms.Button btnDone;
 private System.Windows.Forms.TextBox textBox1;
 private System.Windows.Forms.Button btnAdd;
 private System.Windows.Forms.ListBox listBox1;
 private System.Windows.Forms.ContextMenu contextMenu1;
 private System.Windows.Forms.MenuItem menuItem1;
 private System.Windows.Forms.MenuItem menuItem2;
 private System.Windows.Forms.MenuItem menuItem3;
 private System.Windows.Forms.MenuItem menuItem4;
 private System.ruponentModel.Container components = null;
 public ListBoxContextMenu()
 {
   InitializeComponent();
 }
 protected override void Dispose( bool disposing )
 {
   if( disposing )
   {
     if (components != null) 
     {
       components.Dispose();
     }
   }
   base.Dispose( disposing );
 }
 #region Windows Form Designer generated code
 private void InitializeComponent()
 {
   this.btnDone = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.btnAdd = new System.Windows.Forms.Button();
   this.listBox1 = new System.Windows.Forms.ListBox();
   this.contextMenu1 = new System.Windows.Forms.ContextMenu();
   this.menuItem1 = new System.Windows.Forms.MenuItem();
   this.menuItem2 = new System.Windows.Forms.MenuItem();
   this.menuItem3 = new System.Windows.Forms.MenuItem();
   this.menuItem4 = new System.Windows.Forms.MenuItem();
   this.SuspendLayout();
   // 
   // btnDone
   // 
   this.btnDone.Location = new System.Drawing.Point(208, 16);
   this.btnDone.Name = "btnDone";
   this.btnDone.TabIndex = 3;
   this.btnDone.Text = "Done";
   this.btnDone.Click += new System.EventHandler(this.btnDone_Click);
   // 
   // textBox1
   // 
   this.textBox1.Location = new System.Drawing.Point(16, 88);
   this.textBox1.Name = "textBox1";
   this.textBox1.TabIndex = 5;
   this.textBox1.Text = "";
   // 
   // btnAdd
   // 
   this.btnAdd.Location = new System.Drawing.Point(16, 120);
   this.btnAdd.Name = "btnAdd";
   this.btnAdd.TabIndex = 6;
   this.btnAdd.Text = "Add";
   this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
   // 
   // listBox1
   // 
   this.listBox1.ContextMenu = this.contextMenu1;
   this.listBox1.Location = new System.Drawing.Point(16, 8);
   this.listBox1.Name = "listBox1";
   this.listBox1.Size = new System.Drawing.Size(120, 69);
   this.listBox1.TabIndex = 7;
   // 
   // contextMenu1
   // 
   this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem1,
      this.menuItem2,
      this.menuItem3,
      this.menuItem4});
   // 
   // menuItem1
   // 
   this.menuItem1.Index = 0;
   this.menuItem1.Text = "Move Down";
   this.menuItem1.Click += new System.EventHandler(this.ctxtMenuClick);
   // 
   // menuItem2
   // 
   this.menuItem2.Index = 1;
   this.menuItem2.Text = "Move up";
   this.menuItem2.Click += new System.EventHandler(this.ctxtMenuClick);
   // 
   // menuItem3
   // 
   this.menuItem3.Index = 2;
   this.menuItem3.Text = "Delete";
   this.menuItem3.Click += new System.EventHandler(this.ctxtMenuClick);
   // 
   // menuItem4
   // 
   this.menuItem4.Index = 3;
   this.menuItem4.Text = "Duplicate";
   this.menuItem4.Click += new System.EventHandler(this.ctxtMenuClick);
   // 
   // ListBoxContextMenu
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(292, 266);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
       this.listBox1,
       this.btnAdd,
       this.textBox1,
       this.btnDone});
   this.Name = "ListBoxContextMenu";
   this.ResumeLayout(false);
 }
 #endregion
 [STAThread]
 static void Main() 
 {
   Application.Run(new ListBoxContextMenu());
 }
 private void btnDone_Click(object sender, System.EventArgs e)
 {
   Application.Exit();
 }
 private void btnAdd_Click(object sender, System.EventArgs e)
 {
   listBox1.Items.Add(textBox1.Text);
   textBox1.Text = "";
 }
 private void ctxtMenuClick(object sender, System.EventArgs e)
 {
   if ( listBox1.SelectedIndex != -1 )
   {
     MenuItem mi = (MenuItem) sender;
     MessageBox.Show(mi.Text + " on " + listBox1.SelectedItem,"Context Menu", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
   }else{
     MessageBox.Show("Please select an item","Context Menu Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
   }
 }

}

 </source>


ListBox.DataBindings

<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457

  • /

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; namespace DataBinding101 {

   /// <summary>
   /// Summary description for SingleItemDataBinding.
   /// </summary>
   public class SingleItemDataBinding : System.Windows.Forms.Form
   {
       internal System.Windows.Forms.ListBox lstCity;
       internal System.Windows.Forms.TextBox txtCity;
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public SingleItemDataBinding()
       {
           //
           // Required for Windows Form Designer support
           //
           InitializeComponent();
           //
           // TODO: Add any constructor code after InitializeComponent call
           //
       }
       /// <summary>
       /// Clean up any resources being used.
       /// </summary>
       protected override void Dispose( bool disposing )
       {
           if( disposing )
           {
               if(components != null)
               {
                   components.Dispose();
               }
           }
           base.Dispose( disposing );
       }
       #region Windows Form Designer generated code
       /// <summary>
       /// Required method for Designer support - do not modify
       /// the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
           this.lstCity = new System.Windows.Forms.ListBox();
           this.txtCity = new System.Windows.Forms.TextBox();
           this.SuspendLayout();
           // 
           // lstCity
           // 
           this.lstCity.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
               | System.Windows.Forms.AnchorStyles.Left) 
               | System.Windows.Forms.AnchorStyles.Right);
           this.lstCity.IntegralHeight = false;
           this.lstCity.Location = new System.Drawing.Point(12, 40);
           this.lstCity.Name = "lstCity";
           this.lstCity.Size = new System.Drawing.Size(224, 148);
           this.lstCity.TabIndex = 3;
           // 
           // txtCity
           // 
           this.txtCity.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
               | System.Windows.Forms.AnchorStyles.Right);
           this.txtCity.Location = new System.Drawing.Point(12, 12);
           this.txtCity.Name = "txtCity";
           this.txtCity.Size = new System.Drawing.Size(224, 21);
           this.txtCity.TabIndex = 2;
           this.txtCity.Text = "";
           // 
           // SingleItemDataBinding
           // 
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
           this.ClientSize = new System.Drawing.Size(248, 202);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.lstCity,
                                                                         this.txtCity});
           this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
           this.Name = "SingleItemDataBinding";
           this.Text = "SingleItemDataBinding";
           this.Load += new System.EventHandler(this.SingleItemDataBinding_Load);
           this.ResumeLayout(false);
       }
       #endregion
       private void SingleItemDataBinding_Load(object sender, System.EventArgs e)
       {
            string[] cityChoices = {"Seattle", "New York", "Tokyo", "Montreal"};
           lstCity.DataSource = cityChoices;
           txtCity.DataBindings.Add("Text", cityChoices, "");
       }
       [STAThread]
       static void Main() 
       {
           Application.Run(new SingleItemDataBinding());
       }
   }

}

 </source>


ListBox.DataSource

<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457

  • /

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Drawing.Text; namespace DataBinding101 {

   public class BindingUnusualProperties : System.Windows.Forms.Form
   {
       internal System.Windows.Forms.Label Label2;
       internal System.Windows.Forms.Label Label1;
       internal System.Windows.Forms.ListBox lstFonts;
       internal System.Windows.Forms.Label lblSampleText;
       internal System.Windows.Forms.ListBox lstColors;
       public BindingUnusualProperties()
       {
           this.Label2 = new System.Windows.Forms.Label();
           this.Label1 = new System.Windows.Forms.Label();
           this.lstFonts = new System.Windows.Forms.ListBox();
           this.lblSampleText = new System.Windows.Forms.Label();
           this.lstColors = new System.Windows.Forms.ListBox();
           this.SuspendLayout();
           this.Label2.Location = new System.Drawing.Point(210, 13);
           this.Label2.Size = new System.Drawing.Size(140, 12);
           this.Label2.Text = "Choose a Font:";
           this.Label1.Location = new System.Drawing.Point(14, 13);
           this.Label1.Size = new System.Drawing.Size(140, 12);
           this.Label1.Text = "Choose a Color:";
           this.lstFonts.Location = new System.Drawing.Point(210, 29);
           this.lstFonts.Size = new System.Drawing.Size(180, 134);
           this.lblSampleText.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
               | System.Windows.Forms.AnchorStyles.Left) 
               | System.Windows.Forms.AnchorStyles.Right);
           this.lblSampleText.Location = new System.Drawing.Point(18, 185);
           this.lblSampleText.Name = "lblSampleText";
           this.lblSampleText.Size = new System.Drawing.Size(372, 96);
           this.lblSampleText.TabIndex = 6;
           this.lblSampleText.Text = "Click an item in one of the lists above to change the font or color of this text." +
               " Once the initial conditions are set up (i.e., the binding), this operation happ" +
               "ens automatically.";
           this.lstColors.Location = new System.Drawing.Point(14, 29);
           this.lstColors.Size = new System.Drawing.Size(176, 134);
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
           this.ClientSize = new System.Drawing.Size(404, 294);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.Label2,
                                                                         this.Label1,
                                                                         this.lstFonts,
                                                                         this.lblSampleText,
                                                                         this.lstColors});
           this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
           this.Name = "BindingUnusualProperties";
           this.Text = "Binding Unusual Properties";
           this.Load += new System.EventHandler(this.BindingUnusualProperties_Load);
           this.ResumeLayout(false);
       }
       private void BindingUnusualProperties_Load(object sender, System.EventArgs e)
       {
           ArrayList fontObjList = new ArrayList();
           ArrayList colorObjList = new ArrayList();
           InstalledFontCollection InstalledFonts = new InstalledFontCollection();
           foreach (FontFamily family in InstalledFonts.Families)
           {
               try
               {
                   fontObjList.Add(new Font(family, 12));
               }
               catch
               {
               }
           }
           string[] colorNames;
           colorNames = System.Enum.GetNames(typeof(KnownColor));
           TypeConverter cnvrt = TypeDescriptor.GetConverter(typeof(KnownColor));
           foreach (string colorName in colorNames)
           {
               colorObjList.Add(Color.FromKnownColor((KnownColor)cnvrt.ConvertFromString(colorName)));
           }
           // We can now bind both our list controls.
           lstColors.DataSource = colorObjList;
           lstColors.DisplayMember = "Name";
           lstFonts.DataSource = fontObjList;
           lstFonts.DisplayMember = "Name";
           // The label is bound to both data sources.
           lblSampleText.DataBindings.Add("ForeColor", colorObjList, "");
           lblSampleText.DataBindings.Add("Font", fontObjList, "");
       }
       
       [STAThread]
       static void Main() 
       {
           Application.Run(new BindingUnusualProperties());
       }
   }

}

 </source>


ListBox.DataSourceChanged

<source lang="csharp">

using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.ruponentModel.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 Dispose( bool disposing )
 {
   if( disposing )
   {
     if (components != null) 
     {
       components.Dispose();
     }
   }
   base.Dispose( disposing );
 }
 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(16, 8);
   this.lb.Name = "lb";
   this.lb.Size = new System.Drawing.Size(232, 212);
   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(16, 240);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.TabIndex = 1;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "DataSource";
   // 
   // rbEmployees
   // 
   this.rbEmployees.Location = new System.Drawing.Point(24, 56);
   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(24, 32);
   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(5, 13);
   this.ClientSize = new System.Drawing.Size(264, 389);
   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);
 }

}

 </source>


ListBox.DisplayMember

<source lang="csharp"> /* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457

  • /

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; namespace DataBinding101 {

   /// <summary>
   /// Summary description for ObjectListBinding.
   /// </summary>
   public class ObjectListBinding : System.Windows.Forms.Form
   {
       internal System.Windows.Forms.ListBox lstCity;
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public ObjectListBinding()
       {
           //
           // Required for Windows Form Designer support
           //
           InitializeComponent();
           //
           // TODO: Add any constructor code after InitializeComponent call
           //
       }
       /// <summary>
       /// Clean up any resources being used.
       /// </summary>
       protected override void Dispose( bool disposing )
       {
           if( disposing )
           {
               if(components != null)
               {
                   components.Dispose();
               }
           }
           base.Dispose( disposing );
       }
       #region Windows Form Designer generated code
       /// <summary>
       /// Required method for Designer support - do not modify
       /// the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
           this.lstCity = new System.Windows.Forms.ListBox();
           this.SuspendLayout();
           // 
           // lstCity
           // 
           this.lstCity.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
               | System.Windows.Forms.AnchorStyles.Left) 
               | System.Windows.Forms.AnchorStyles.Right);
           this.lstCity.IntegralHeight = false;
           this.lstCity.Location = new System.Drawing.Point(4, 4);
           this.lstCity.Name = "lstCity";
           this.lstCity.Size = new System.Drawing.Size(252, 216);
           this.lstCity.TabIndex = 2;
           this.lstCity.DoubleClick += new System.EventHandler(this.lstCity_DoubleClick);
           // 
           // ObjectListBinding
           // 
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
           this.ClientSize = new System.Drawing.Size(268, 230);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.lstCity});
           this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
           this.Name = "ObjectListBinding";
           this.Text = "ObjectListBinding";
           this.Load += new System.EventHandler(this.ObjectListBinding_Load);
           this.ResumeLayout(false);
       }
       #endregion
       private void ObjectListBinding_Load(object sender, System.EventArgs e)
       {
           City[] cityChoices = {new City("Seattle", "U.S.A."), 
                                    new City("New York", "U.S.A."), new City("Tokyo", "Japan"), 
                                    new City("Montreal", "Canada")};
           lstCity.DataSource = cityChoices;
           lstCity.DisplayMember = "Name";
       }
       private void lstCity_DoubleClick(object sender, System.EventArgs e)
       {
               MessageBox.Show(((City)lstCity.SelectedItem).Country);
       }
       [STAThread]
       static void Main() 
       {
           Application.Run(new ObjectListBinding());
       }
   }
   public class City
   {
       private string name;
       private string country;
       public string Name
       {
           get
           {
               return name;
           }
           set
           {
               name = value;
           }
       }
       public string Country
       {
           get
           {
               return country;
           }
           set
           {
               country = value;
           }
       }
       public City(string name, string country)
       {
           this.Name = name;
           this.Country = country;
       }
   }

}

 </source>


ListBox.DisplayMemberChanged

<source lang="csharp">

using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.ruponentModel.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 Dispose( bool disposing )
 {
   if( disposing )
   {
     if (components != null) 
     {
       components.Dispose();
     }
   }
   base.Dispose( disposing );
 }
 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(16, 8);
   this.lb.Name = "lb";
   this.lb.Size = new System.Drawing.Size(232, 212);
   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(16, 240);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.TabIndex = 1;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "DataSource";
   // 
   // rbEmployees
   // 
   this.rbEmployees.Location = new System.Drawing.Point(24, 56);
   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(24, 32);
   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(5, 13);
   this.ClientSize = new System.Drawing.Size(264, 389);
   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);
 }

}

 </source>


ListBox.EndUpdate()

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class ListBoxItemAddRange : Form{

 ListBox lb;
 public ListBoxItemAddRange()
 {
   Size = new Size(300,400);
   lb = new ListBox();
   lb.Parent = this;
   lb.Location = new Point(10,10);
   lb.Size = new Size(ClientSize.Width - 20, Height - 200);
   lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
   lb.BorderStyle = BorderStyle.Fixed3D;
   lb.BeginUpdate();
   string[] arNames = new string[5];
   for(int i = 0;i<5;i++){
      arNames[i] = "I";    
   }
   lb.Items.AddRange(arNames);
   lb.Items.Add("12345");
   lb.Items.Add("67890");
   lb.EndUpdate();
 } 
 static void Main() 
 {
   Application.Run(new ListBoxItemAddRange());
 }

}

 </source>


ListBox.FindString

<source lang="csharp"> /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794

  • /

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace AddControls {

 /// <summary>
 /// Summary description for AddControls.
 /// </summary>
 public class AddControls : System.Windows.Forms.Form
 {
   private System.Windows.Forms.ListBox listBox1;
   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.TextBox textBox1;
   private System.Windows.Forms.Button button2;
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ruponentModel.Container components = null;
   public AddControls()
   {
     //
     // Required for Windows Form Designer support
     //
     InitializeComponent();
     //
     // TODO: Add any constructor code after InitializeComponent call
     //
   }
   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   protected override void Dispose( bool disposing )
   {
     if( disposing )
     {
       if (components != null) 
       {
         components.Dispose();
       }
     }
     base.Dispose( disposing );
   }
   #region Windows Form Designer generated code
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
     this.button2 = new System.Windows.Forms.Button();
     this.textBox1 = new System.Windows.Forms.TextBox();
     this.button1 = new System.Windows.Forms.Button();
     this.listBox1 = new System.Windows.Forms.ListBox();
     this.SuspendLayout();
     // 
     // button2
     // 
     this.button2.Location = new System.Drawing.Point(160, 240);
     this.button2.Name = "button2";
     this.button2.Size = new System.Drawing.Size(96, 24);
     this.button2.TabIndex = 3;
     this.button2.Text = "Cancel";
     this.button2.Click += new System.EventHandler(this.button2_Click);
     // 
     // textBox1
     // 
     this.textBox1.Location = new System.Drawing.Point(38, 200);
     this.textBox1.Name = "textBox1";
     this.textBox1.Size = new System.Drawing.Size(216, 20);
     this.textBox1.TabIndex = 1;
     this.textBox1.Text = "";
     // 
     // button1
     // 
     this.button1.Location = new System.Drawing.Point(48, 240);
     this.button1.Name = "button1";
     this.button1.Size = new System.Drawing.Size(80, 24);
     this.button1.TabIndex = 2;
     this.button1.Text = "Add Item";
     this.button1.Click += new System.EventHandler(this.button1_Click);
     // 
     // listBox1
     // 
     this.listBox1.Location = new System.Drawing.Point(38, 32);
     this.listBox1.Name = "listBox1";
     this.listBox1.Size = new System.Drawing.Size(216, 147);
     this.listBox1.TabIndex = 0;
     // 
     // Form1
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                     this.button2,
                                     this.button1,
                                     this.textBox1,
                                     this.listBox1});
     this.Name = "AddControls";
     this.Text = "AddControls";
     this.ResumeLayout(false);
   }
   #endregion
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main() 
   {
     Application.Run(new AddControls());
   }
   private void button1_Click(object sender, System.EventArgs e)
   {
     if (textBox1.Text == "")
       return;
     string strAdd = textBox1.Text;
     if (listBox1.FindString (strAdd, -1) < 0)
     {
       listBox1.Items.Add (strAdd);
       textBox1.Text = "";
       textBox1.Focus ();
       return;
     }
     MessageBox.Show ("\"" + strAdd + "\" is already in the list box", "Duplicate");
   }
   private void button2_Click(object sender, System.EventArgs e)
   {
     Application.Exit();
   }
 }

}


 </source>


ListBox.Items

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {

     private System.Windows.Forms.ListBox displayListBox;
     private System.Windows.Forms.TextBox inputTextBox;
     private System.Windows.Forms.Button addButton;
     private System.Windows.Forms.Button removeButton;
     private System.Windows.Forms.Button clearButton;
     
     public Form1() {
       InitializeComponent();
   }
     private void addButton_Click( object sender, EventArgs e )
     {
       displayListBox.Items.Add( inputTextBox.Text );
       inputTextBox.Clear();
     } 
     private void removeButton_Click( object sender, EventArgs e )
     {
       if ( displayListBox.SelectedIndex != -1 )
         displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
     }
     private void clearButton_Click( object sender, EventArgs e )
     {
        displayListBox.Items.Clear();
     }
     private void InitializeComponent()
     {
        this.displayListBox = new System.Windows.Forms.ListBox();
        this.inputTextBox = new System.Windows.Forms.TextBox();
        this.addButton = new System.Windows.Forms.Button();
        this.removeButton = new System.Windows.Forms.Button();
        this.clearButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // displayListBox
        // 
        this.displayListBox.FormattingEnabled = true;
        this.displayListBox.Location = new System.Drawing.Point(13, 12);
        this.displayListBox.Name = "displayListBox";
        this.displayListBox.Size = new System.Drawing.Size(119, 238);
        this.displayListBox.TabIndex = 0;
        // 
        // inputTextBox
        // 
        this.inputTextBox.Location = new System.Drawing.Point(149, 12);
        this.inputTextBox.Name = "inputTextBox";
        this.inputTextBox.Size = new System.Drawing.Size(100, 20);
        this.inputTextBox.TabIndex = 1;
        // 
        // addButton
        // 
        this.addButton.Location = new System.Drawing.Point(149, 56);
        this.addButton.Name = "addButton";
        this.addButton.Size = new System.Drawing.Size(100, 36);
        this.addButton.TabIndex = 2;
        this.addButton.Text = "Add";
        this.addButton.Click += new System.EventHandler(this.addButton_Click);
        // 
        // removeButton
        // 
        this.removeButton.Location = new System.Drawing.Point(149, 109);
        this.removeButton.Name = "removeButton";
        this.removeButton.Size = new System.Drawing.Size(100, 36);
        this.removeButton.TabIndex = 3;
        this.removeButton.Text = "Remove";
        this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
        // 
        // clearButton
        // 
        this.clearButton.Location = new System.Drawing.Point(149, 165);
        this.clearButton.Name = "clearButton";
        this.clearButton.Size = new System.Drawing.Size(100, 36);
        this.clearButton.TabIndex = 4;
        this.clearButton.Text = "Clear";
        this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
        // ListBoxTestForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(263, 268);
        this.Controls.Add(this.clearButton);
        this.Controls.Add(this.removeButton);
        this.Controls.Add(this.addButton);
        this.Controls.Add(this.inputTextBox);
        this.Controls.Add(this.displayListBox);
        this.Name = "ListBoxTestForm";
        this.Text = "ListBoxTest";
        this.ResumeLayout(false);
        this.PerformLayout();
     }
     [STAThread]
     static void Main()
     {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
   }

}


 </source>


ListBox.Items.Add

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {

 private System.Windows.Forms.ListBox lstCustomers;
 public Form1() {
       InitializeComponent();
   lstCustomers.Items.Add(new Customer("A", "B", DateTime.Now.AddDays(-10)));
   lstCustomers.Items.Add(new Customer("C", "D", DateTime.Now.AddDays(-100)));
   lstCustomers.Items.Add(new Customer("F", "G", DateTime.Now.AddDays(-500)));
 }
 private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
 {
   Customer cust = (Customer)lstCustomers.SelectedItem;
   MessageBox.Show("Birth Date: " + cust.BirthDate.ToShortDateString());
 }
 private void InitializeComponent()
 {
   this.lstCustomers = new System.Windows.Forms.ListBox();
   this.SuspendLayout();
   // 
   // lstCustomers
   // 
   this.lstCustomers.FormattingEnabled = true;
   this.lstCustomers.Location = new System.Drawing.Point(12, 12);
   this.lstCustomers.Name = "lstCustomers";
   this.lstCustomers.Size = new System.Drawing.Size(120, 95);
   this.lstCustomers.TabIndex = 0;
   this.lstCustomers.SelectedIndexChanged += new System.EventHandler(this.lstCustomers_SelectedIndexChanged);
   // 
   // Form1
   // 
   this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
   this.ClientSize = new System.Drawing.Size(308, 230);
   this.Controls.Add(this.lstCustomers);
   this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);
 }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

} public class Customer {

 public string FirstName;
 public string LastName;
 public DateTime BirthDate;
 public Customer() { }
 public Customer(string firstName, string lastName, DateTime birthDate)
 {
   FirstName = firstName;
   LastName = lastName;
   BirthDate = birthDate;
 }
 public override string ToString()
 {
   return FirstName + " " + LastName;
 }

}



 </source>


ListBox.Items.AddRange

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {

 private System.Windows.Forms.Label lblSaturation;
 private System.Windows.Forms.Label lblHue;
 private System.Windows.Forms.Label lblBrightness;
 private System.Windows.Forms.Label Label1;
 private System.Windows.Forms.ListBox lstColors;
 public Form1()
 {
   InitializeComponent();
   string[] colorNames  = System.Enum.GetNames(typeof(KnownColor));
   lstColors.Items.AddRange(colorNames);
 }
 private void lstColors_SelectedIndexChanged(object sender, EventArgs e)
 {
   KnownColor selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), lstColors.Text);
   this.BackColor = System.Drawing.Color.FromKnownColor(selectedColor);
   lblBrightness.Text = "Brightness = " + this.BackColor.GetBrightness().ToString();
   lblHue.Text = "Hue = " + this.BackColor.GetHue().ToString();
   lblSaturation.Text = "Saturation = " + this.BackColor.GetSaturation().ToString();
 }
 private void InitializeComponent()
 {
   this.lblSaturation = new System.Windows.Forms.Label();
   this.lblHue = new System.Windows.Forms.Label();
   this.lblBrightness = new System.Windows.Forms.Label();
   this.Label1 = new System.Windows.Forms.Label();
   this.lstColors = new System.Windows.Forms.ListBox();
   this.SuspendLayout();
   // 
   // lblSaturation
   // 
   this.lblSaturation.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
   this.lblSaturation.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.lblSaturation.FlatStyle = System.Windows.Forms.FlatStyle.System;
   this.lblSaturation.Location = new System.Drawing.Point(268, 57);
   this.lblSaturation.Name = "lblSaturation";
   this.lblSaturation.Size = new System.Drawing.Size(136, 20);
   this.lblSaturation.TabIndex = 4;
   this.lblSaturation.Text = " Saturation";
   // 
   // lblHue
   // 
   this.lblHue.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
   this.lblHue.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.lblHue.FlatStyle = System.Windows.Forms.FlatStyle.System;
   this.lblHue.Location = new System.Drawing.Point(268, 33);
   this.lblHue.Name = "lblHue";
   this.lblHue.Size = new System.Drawing.Size(136, 20);
   this.lblHue.TabIndex = 3;
   this.lblHue.Text = " Hue";
   // 
   // lblBrightness
   // 
   this.lblBrightness.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
   this.lblBrightness.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.lblBrightness.FlatStyle = System.Windows.Forms.FlatStyle.System;
   this.lblBrightness.Location = new System.Drawing.Point(268, 9);
   this.lblBrightness.Name = "lblBrightness";
   this.lblBrightness.Size = new System.Drawing.Size(136, 20);
   this.lblBrightness.TabIndex = 2;
   this.lblBrightness.Text = " Brightness";
   // 
   // Label1
   // 
   this.Label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
   this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
   this.Label1.Location = new System.Drawing.Point(12, 9);
   this.Label1.Name = "Label1";
   this.Label1.Size = new System.Drawing.Size(200, 20);
   this.Label1.TabIndex = 0;
   this.Label1.Text = " Choose a Background Color:";
   // 
   // lstColors
   // 
   this.lstColors.FormattingEnabled = true;
   this.lstColors.Location = new System.Drawing.Point(12, 37);
   this.lstColors.Name = "lstColors";
   this.lstColors.Size = new System.Drawing.Size(200, 238);
   this.lstColors.TabIndex = 1;
   this.lstColors.SelectedIndexChanged += new System.EventHandler(this.lstColors_SelectedIndexChanged);
   // 
   // Form1
   // 
   this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
   this.ClientSize = new System.Drawing.Size(430, 284);
   this.Controls.Add(this.lblSaturation);
   this.Controls.Add(this.lblHue);
   this.Controls.Add(this.lblBrightness);
   this.Controls.Add(this.Label1);
   this.Controls.Add(this.lstColors);
   this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.Name = "Form1";
   this.Text = "Color Changer";
   this.ResumeLayout(false);
 }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


 </source>


ListBox.Items.Clear()

<source lang="csharp">

using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {

     private System.Windows.Forms.ListBox displayListBox;
     private System.Windows.Forms.TextBox inputTextBox;
     private System.Windows.Forms.Button addButton;
     private System.Windows.Forms.Button removeButton;
     private System.Windows.Forms.Button clearButton;
     
     public Form1() {
       InitializeComponent();
   }
     private void addButton_Click( object sender, EventArgs e )
     {
       displayListBox.Items.Add( inputTextBox.Text );
       inputTextBox.Clear();
     } 
     private void removeButton_Click( object sender, EventArgs e )
     {
       if ( displayListBox.SelectedIndex != -1 )
         displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
     }
     private void clearButton_Click( object sender, EventArgs e )
     {
        displayListBox.Items.Clear();
     }
     private void InitializeComponent()
     {
        this.displayListBox = new System.Windows.Forms.ListBox();
        this.inputTextBox = new System.Windows.Forms.TextBox();
        this.addButton = new System.Windows.Forms.Button();
        this.removeButton = new System.Windows.Forms.Button();
        this.clearButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // displayListBox
        // 
        this.displayListBox.FormattingEnabled = true;
        this.displayListBox.Location = new System.Drawing.Point(13, 12);
        this.displayListBox.Name = "displayListBox";
        this.displayListBox.Size = new System.Drawing.Size(119, 238);
        this.displayListBox.TabIndex = 0;
        // 
        // inputTextBox
        // 
        this.inputTextBox.Location = new System.Drawing.Point(149, 12);
        this.inputTextBox.Name = "inputTextBox";
        this.inputTextBox.Size = new System.Drawing.Size(100, 20);
        this.inputTextBox.TabIndex = 1;
        // 
        // addButton
        // 
        this.addButton.Location = new System.Drawing.Point(149, 56);
        this.addButton.Name = "addButton";
        this.addButton.Size = new System.Drawing.Size(100, 36);
        this.addButton.TabIndex = 2;
        this.addButton.Text = "Add";
        this.addButton.Click += new System.EventHandler(this.addButton_Click);
        // 
        // removeButton
        // 
        this.removeButton.Location = new System.Drawing.Point(149, 109);
        this.removeButton.Name = "removeButton";
        this.removeButton.Size = new System.Drawing.Size(100, 36);
        this.removeButton.TabIndex = 3;
        this.removeButton.Text = "Remove";
        this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
        // 
        // clearButton
        // 
        this.clearButton.Location = new System.Drawing.Point(149, 165);
        this.clearButton.Name = "clearButton";
        this.clearButton.Size = new System.Drawing.Size(100, 36);
        this.clearButton.TabIndex = 4;
        this.clearButton.Text = "Clear";
        this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
        // ListBoxTestForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(263, 268);
        this.Controls.Add(this.clearButton);
        this.Controls.Add(this.removeButton);
        this.Controls.Add(this.addButton);
        this.Controls.Add(this.inputTextBox);
        this.Controls.Add(this.displayListBox);
        this.Name = "ListBoxTestForm";
        this.Text = "ListBoxTest";
        this.ResumeLayout(false);
        this.PerformLayout();
     }
     [STAThread]
     static void Main()
     {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
   }

}



 </source>


ListBox.Items.RemoveAt

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {

     private System.Windows.Forms.ListBox displayListBox;
     private System.Windows.Forms.TextBox inputTextBox;
     private System.Windows.Forms.Button addButton;
     private System.Windows.Forms.Button removeButton;
     private System.Windows.Forms.Button clearButton;
     
     public Form1() {
       InitializeComponent();
   }
     private void addButton_Click( object sender, EventArgs e )
     {
       displayListBox.Items.Add( inputTextBox.Text );
       inputTextBox.Clear();
     } 
     private void removeButton_Click( object sender, EventArgs e )
     {
       if ( displayListBox.SelectedIndex != -1 )
         displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
     }
     private void clearButton_Click( object sender, EventArgs e )
     {
        displayListBox.Items.Clear();
     }
     private void InitializeComponent()
     {
        this.displayListBox = new System.Windows.Forms.ListBox();
        this.inputTextBox = new System.Windows.Forms.TextBox();
        this.addButton = new System.Windows.Forms.Button();
        this.removeButton = new System.Windows.Forms.Button();
        this.clearButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // displayListBox
        // 
        this.displayListBox.FormattingEnabled = true;
        this.displayListBox.Location = new System.Drawing.Point(13, 12);
        this.displayListBox.Name = "displayListBox";
        this.displayListBox.Size = new System.Drawing.Size(119, 238);
        this.displayListBox.TabIndex = 0;
        // 
        // inputTextBox
        // 
        this.inputTextBox.Location = new System.Drawing.Point(149, 12);
        this.inputTextBox.Name = "inputTextBox";
        this.inputTextBox.Size = new System.Drawing.Size(100, 20);
        this.inputTextBox.TabIndex = 1;
        // 
        // addButton
        // 
        this.addButton.Location = new System.Drawing.Point(149, 56);
        this.addButton.Name = "addButton";
        this.addButton.Size = new System.Drawing.Size(100, 36);
        this.addButton.TabIndex = 2;
        this.addButton.Text = "Add";
        this.addButton.Click += new System.EventHandler(this.addButton_Click);
        // 
        // removeButton
        // 
        this.removeButton.Location = new System.Drawing.Point(149, 109);
        this.removeButton.Name = "removeButton";
        this.removeButton.Size = new System.Drawing.Size(100, 36);
        this.removeButton.TabIndex = 3;
        this.removeButton.Text = "Remove";
        this.removeButton.Click += new System.EventHandler(this.removeButton_Click);
        // 
        // clearButton
        // 
        this.clearButton.Location = new System.Drawing.Point(149, 165);
        this.clearButton.Name = "clearButton";
        this.clearButton.Size = new System.Drawing.Size(100, 36);
        this.clearButton.TabIndex = 4;
        this.clearButton.Text = "Clear";
        this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
        // ListBoxTestForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(263, 268);
        this.Controls.Add(this.clearButton);
        this.Controls.Add(this.removeButton);
        this.Controls.Add(this.addButton);
        this.Controls.Add(this.inputTextBox);
        this.Controls.Add(this.displayListBox);
        this.Name = "ListBoxTestForm";
        this.Text = "ListBoxTest";
        this.ResumeLayout(false);
        this.PerformLayout();
     }
     [STAThread]
     static void Main()
     {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
   }

}


 </source>


ListBox.ScrollAlwaysVisible

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class ListBoxSelectionMode : Form {

 ListBox lb;
 RadioButton rdoMultiExtended;
 RadioButton rdoMultiSimple;
 RadioButton rdoMultiOne;
 TextBox txtTop;
 Button btnTop;
 public ListBoxSelectionMode()
 {
   int xSize, ySize;
   Size = new Size(300,400);
   lb = new ListBox();
   lb.Parent = this;
   lb.Location = new Point(10,10);
   lb.Size = new Size(ClientSize.Width - 20, Height - 200);
   lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
   lb.BorderStyle = BorderStyle.Fixed3D;
   lb.MultiColumn = true;
   lb.ScrollAlwaysVisible = true;
   GroupBox grpMulti = new GroupBox();
   grpMulti.Parent = this;
   grpMulti.Text = "MultiSelect";
   grpMulti.Location = new Point(lb.Left, lb.Bottom + 25);
   grpMulti.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
   rdoMultiOne = new RadioButton();
   rdoMultiOne.Parent = grpMulti;
   rdoMultiOne.Text = "One";
   rdoMultiOne.Tag = SelectionMode.One;
   rdoMultiOne.Checked = true;
   rdoMultiOne.Location = new Point(10,15);
   rdoMultiOne.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
   rdoMultiSimple = new RadioButton();
   rdoMultiSimple.Parent = grpMulti;
   rdoMultiSimple.Text = "Multi-Simple";
   rdoMultiSimple.Tag = SelectionMode.MultiSimple;
   rdoMultiSimple.Location = new Point(10, rdoMultiOne.Bottom);
   rdoMultiSimple.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
   rdoMultiExtended = new RadioButton();
   rdoMultiExtended.Parent = grpMulti;
   rdoMultiExtended.Text = "Multi-Extended";
   rdoMultiExtended.Tag = SelectionMode.MultiExtended;
   rdoMultiExtended.Location = new Point(10, rdoMultiSimple.Bottom);
   rdoMultiExtended.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
   xSize = (int)(Font.Height * .75) * rdoMultiExtended.Text.Length;
   ySize = ((int)rdoMultiOne.Height * 3) + 20;
   grpMulti.Size = new Size(xSize, ySize);
   Panel pnlTop = new Panel();
   pnlTop.Parent = this;
   pnlTop.Location = new Point(lb.Left, grpMulti.Bottom + 10);
   pnlTop.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
   Label lblTop = new Label();
   lblTop.Parent = pnlTop;
   lblTop.Text = "TopIndex: ";
   xSize = ((int)(Font.Height * .5) * lblTop.Text.Length);
   lblTop.Size = new Size(xSize, Font.Height + 10);
   txtTop = new TextBox();
   txtTop.Parent = pnlTop;
   txtTop.Location = new Point(lblTop.Right, lblTop.Top);
   txtTop.Text = lb.TopIndex.ToString();
   txtTop.Size = new Size((int)(Font.Height * .75) * 3, 
               Font.Height + 10);
   btnTop = new Button();
   btnTop.Parent = pnlTop;
   btnTop.Text = "Update";
   btnTop.Location = new Point(txtTop.Right + 10, txtTop.Top);
   btnTop.Click += new System.EventHandler(btnTop_Click);
   lb.Items.Add("12345");
     lb.Items.Add("67890");      
     lb.Items.Add("7890");      
     lb.Items.Add("890");            
 } 
 static void Main() 
 {
   Application.Run(new ListBoxSelectionMode());
 }
 private void rdoMulti_CheckedChanged(object sender, EventArgs e)
 {
   RadioButton rdo = (RadioButton)sender;
   lb.SelectionMode = (SelectionMode)rdo.Tag;
 }
 private void btnTop_Click(object sender, EventArgs e)
 {
   txtTop.Text = lb.TopIndex.ToString();
 }

}

 </source>


ListBox.SelectedIndex

<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;

public class ListBoxDemo : System.Windows.Forms.Form {

   private System.ruponentModel.Container container;
   private System.Windows.Forms.Button buttonAdd;
   private System.Windows.Forms.Button buttonClose;
   private System.Windows.Forms.Button buttonModify;
   private System.Windows.Forms.Button buttonDelete;
   private System.Windows.Forms.Button buttonMoveUp;
   private System.Windows.Forms.Button buttonMoveDown;
   private System.Windows.Forms.ListBox listbox;
   private System.Windows.Forms.TextBox textbox;
   private System.Windows.Forms.Label label;
   private int nSelectedIndex;
   //*********SIZE & LOCATION******************//
   // COMPONENT - BUTTON(s) aligned along X-axis.
   const int BUTTON_LENGTH = 50;
   const int BUTTON_HEIGHT = 20;
   const int FIRSTBUTTON_XPOS = 20;
   const int FIRSTBUTTON_YPOS =220;
   const int XSPACING = 70; // (Note: XSPACING >= BUTTON_LENGTH)
   const int YSPACING =  0;
   //COMPONENT - MOVE BUTTONS
   const int MBUTTON_LENGTH = 20;
   const int MBUTTON_HEIGHT = 20;
   const int FIRSTMBUTTON_XPOS = 220;
   const int FIRSTMBUTTON_YPOS =70;
   const int SECONDMBUTTON_XPOS = 220;
   const int SECONDMBUTTON_YPOS =100;
   // COMPONENT - LISTBOX
   const int LISTBOX_LENGTH = 3*BUTTON_LENGTH;
   const int LISTBOX_HEIGHT = 6*BUTTON_HEIGHT;
   const int LISTBOX_XPOS = 50;
   const int LISTBOX_YPOS = 50;
   // COMPONENT - LABEL
   const int LABEL_LENGTH = 50;
   const int LABEL_HEIGHT = 50;
   const int LABEL_XPOS = 20; // align it with first button
   const int LABEL_YPOS = 173;
   // COMPONENT - TEXTBOX
   const int TEXTBOX_LENGTH = 120;
   const int TEXTBOX_HEIGHT = 50;
   const int TEXTBOX_XPOS =  70;
   const int TEXTBOX_YPOS = 170;
   public ListBoxDemo() : base() {
       InitializeComponent();
   }
   private void InitializeComponent() {
       // this
       this.container = new System.ruponentModel.Container();
       this.Text="List Box";
       // buttonAdd
       this.buttonAdd = new System.Windows.Forms.Button();
       buttonAdd.Location = new
           System.Drawing.Point(FIRSTBUTTON_XPOS,FIRSTBUTTON_YPOS);
       buttonAdd.Text = "&Add";
       buttonAdd.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
       buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
       buttonAdd.Enabled = false;
       this.Controls.Add(this.buttonAdd);
       //buttonModify
       this.buttonModify = new System.Windows.Forms.Button();
       buttonModify.Location = new
           System.Drawing.Point(FIRSTBUTTON_XPOS+XSPACING,FIRSTBUTTON_YPOS+YSPACING);
       buttonModify.Text = "&Modify";
       buttonModify.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
       buttonModify.Click += new System.EventHandler(this.buttonModify_Click);
       buttonModify.Enabled = false;
       this.Controls.Add(this.buttonModify);
       //buttonDelete
       this.buttonDelete = new System.Windows.Forms.Button();
       buttonDelete.Location = new
           System.Drawing.Point(FIRSTBUTTON_XPOS+2*XSPACING,FIRSTBUTTON_YPOS+2*YSPACING);
       buttonDelete.Text = "&Delete";
       buttonDelete.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
       buttonDelete.Enabled = false;
       buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
       this.Controls.Add(this.buttonDelete);
       // buttonClose
       this.buttonClose = new System.Windows.Forms.Button();
       buttonClose.Location = new
           System.Drawing.Point(FIRSTBUTTON_XPOS+3*XSPACING,FIRSTBUTTON_YPOS+3*YSPACING);
       buttonClose.Text = "&Close";
       buttonClose.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
       buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
       this.Controls.Add(this.buttonClose);
       // listbox
       this.listbox = new System.Windows.Forms.ListBox();
       listbox.Location = new System.Drawing.Point(LISTBOX_XPOS,LISTBOX_YPOS);
       listbox.Size = new System.Drawing.Size(LISTBOX_LENGTH,LISTBOX_HEIGHT);
       listbox.Click += new 
       
       System.EventHandler(this.listbox_SelectedIndexChanged);
       listbox.BackColor = (Color)System.Drawing.SystemColors.Desktop;
       this.Controls.Add(this.listbox);
       // label
       this.label = new System.Windows.Forms.Label();
       label.Location = new System.Drawing.Point(LABEL_XPOS,LABEL_YPOS);
       label.Size = new System.Drawing.Size(LABEL_LENGTH,LABEL_HEIGHT);
       label.Text = "Enter:";
       this.Controls.Add(this.label);
       // textbox
       this.textbox = new System.Windows.Forms.TextBox();
       textbox.Location = new System.Drawing.Point(TEXTBOX_XPOS,TEXTBOX_YPOS);
       textbox.Click += new System.EventHandler(this.textbox_Click);
       textbox.Size = new System.Drawing.Size(TEXTBOX_LENGTH,TEXTBOX_HEIGHT);
       this.Controls.Add(this.textbox);
       // buttonMoveUp
       this.buttonMoveUp = new System.Windows.Forms.Button();
       buttonMoveUp.Location = new
           System.Drawing.Point(FIRSTMBUTTON_XPOS,FIRSTMBUTTON_YPOS);
       buttonMoveUp.Text = "<";
       buttonMoveUp.Size = new 
       
       System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
       buttonMoveUp.Click += new System.EventHandler(this.buttonMoveUp_Click);
       buttonMoveUp.Enabled = false;
       this.Controls.Add(this.buttonMoveUp);
       // buttonMoveDown
       this.buttonMoveDown = new System.Windows.Forms.Button();
       buttonMoveDown.Location = new
           System.Drawing.Point(SECONDMBUTTON_XPOS,SECONDMBUTTON_YPOS);
       buttonMoveDown.Text = ">";
       buttonMoveDown.Size = new 
       
       System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
       buttonMoveDown.Click += new 
       System.EventHandler(this.buttonMoveDown_Click);
       buttonMoveDown.Enabled = false;
       this.Controls.Add(this.buttonMoveDown);
   }
   protected void textbox_Click(Object sender, System.EventArgs e) {
       this.buttonAdd.Enabled = true;
       if (this.listbox.Items.Count>0)
           EnableAllListBoxButtons();
   }
   protected void listbox_SelectedIndexChanged(object sender, System.EventArgs e) {
       nSelectedIndex = this.listbox.SelectedIndex;
       string szSelected = (string)this.listbox.SelectedItem;
       this.textbox.Text = szSelected;
   }
   protected void buttonAdd_Click(Object sender, System.EventArgs e) {
       if (this.textbox.Text !="") {
       this.listbox.Items.Add(this.textbox.Text);
       this.textbox.Text = "";
       EnableAllListBoxButtons();
       }
   }
   protected void buttonModify_Click(Object sender, System.EventArgs e) {
       this.listbox.Items[nSelectedIndex] = this.textbox.Text;
   }
   protected void buttonDelete_Click(Object sender, System.EventArgs e) {
       nSelectedIndex = this.listbox.SelectedIndex;
       this.listbox.Items.Remove(nSelectedIndex);
       System.Console.WriteLine("Remove fn does not work...");
   }
   protected void buttonClose_Click(Object sender, System.EventArgs e) {
       this.Close();
   }
   protected void buttonMoveUp_Click(Object sender, System.EventArgs e) {
       if (this.listbox.SelectedIndex >0)
           this.listbox.SelectedIndex--;
   }
   protected void buttonMoveDown_Click(Object sender, System.EventArgs e) {
       if (this.listbox.SelectedIndex < this.listbox.Items.Count-1)
           this.listbox.SelectedIndex++;
   }
   private void EnableAllListBoxButtons() {
       this.buttonAdd.Enabled = true;
       this.buttonModify.Enabled = true;
       this.buttonDelete.Enabled = true;
       this.buttonMoveUp.Enabled = true;
       this.buttonMoveDown.Enabled = true;
   }
   [STAThread]
   public static void Main(string[] args) {
       Application.Run(new ListBoxDemo());
   }

}


 </source>


ListBox.SelectedIndexChanged

<source lang="csharp">

using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {

 private System.Windows.Forms.ListBox lstCustomers;
 public Form1() {
       InitializeComponent();
   lstCustomers.Items.Add(new Customer("A", "B", DateTime.Now.AddDays(-10)));
   lstCustomers.Items.Add(new Customer("C", "D", DateTime.Now.AddDays(-100)));
   lstCustomers.Items.Add(new Customer("F", "G", DateTime.Now.AddDays(-500)));
 }
 private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
 {
   Customer cust = (Customer)lstCustomers.SelectedItem;
   MessageBox.Show("Birth Date: " + cust.BirthDate.ToShortDateString());
 }
 private void InitializeComponent()
 {
   this.lstCustomers = new System.Windows.Forms.ListBox();
   this.SuspendLayout();
   // 
   // lstCustomers
   // 
   this.lstCustomers.FormattingEnabled = true;
   this.lstCustomers.Location = new System.Drawing.Point(12, 12);
   this.lstCustomers.Name = "lstCustomers";
   this.lstCustomers.Size = new System.Drawing.Size(120, 95);
   this.lstCustomers.TabIndex = 0;
   this.lstCustomers.SelectedIndexChanged += new System.EventHandler(this.lstCustomers_SelectedIndexChanged);
   // 
   // Form1
   // 
   this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
   this.ClientSize = new System.Drawing.Size(308, 230);
   this.Controls.Add(this.lstCustomers);
   this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);
 }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

} public class Customer {

 public string FirstName;
 public string LastName;
 public DateTime BirthDate;
 public Customer() { }
 public Customer(string firstName, string lastName, DateTime birthDate)
 {
   FirstName = firstName;
   LastName = lastName;
   BirthDate = birthDate;
 }
 public override string ToString()
 {
   return FirstName + " " + LastName;
 }

}


 </source>


ListBox.SelectedValueChanged

<source lang="csharp">

using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.ruponentModel.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 Dispose( bool disposing )
 {
   if( disposing )
   {
     if (components != null) 
     {
       components.Dispose();
     }
   }
   base.Dispose( disposing );
 }
 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(16, 8);
   this.lb.Name = "lb";
   this.lb.Size = new System.Drawing.Size(232, 212);
   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(16, 240);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.TabIndex = 1;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "DataSource";
   // 
   // rbEmployees
   // 
   this.rbEmployees.Location = new System.Drawing.Point(24, 56);
   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(24, 32);
   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(5, 13);
   this.ClientSize = new System.Drawing.Size(264, 389);
   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);
 }

}

 </source>


ListBox.SelectionMode

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class ListBoxSelectionMode : Form {

 ListBox lb;
 RadioButton rdoMultiExtended;
 RadioButton rdoMultiSimple;
 RadioButton rdoMultiOne;
 TextBox txtTop;
 Button btnTop;
 public ListBoxSelectionMode()
 {
   int xSize, ySize;
   Size = new Size(300,400);
   lb = new ListBox();
   lb.Parent = this;
   lb.Location = new Point(10,10);
   lb.Size = new Size(ClientSize.Width - 20, Height - 200);
   lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
   lb.BorderStyle = BorderStyle.Fixed3D;
   lb.MultiColumn = true;
   lb.ScrollAlwaysVisible = true;
   GroupBox grpMulti = new GroupBox();
   grpMulti.Parent = this;
   grpMulti.Text = "MultiSelect";
   grpMulti.Location = new Point(lb.Left, lb.Bottom + 25);
   grpMulti.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
   rdoMultiOne = new RadioButton();
   rdoMultiOne.Parent = grpMulti;
   rdoMultiOne.Text = "One";
   rdoMultiOne.Tag = SelectionMode.One;
   rdoMultiOne.Checked = true;
   rdoMultiOne.Location = new Point(10,15);
   rdoMultiOne.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
   rdoMultiSimple = new RadioButton();
   rdoMultiSimple.Parent = grpMulti;
   rdoMultiSimple.Text = "Multi-Simple";
   rdoMultiSimple.Tag = SelectionMode.MultiSimple;
   rdoMultiSimple.Location = new Point(10, rdoMultiOne.Bottom);
   rdoMultiSimple.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
   rdoMultiExtended = new RadioButton();
   rdoMultiExtended.Parent = grpMulti;
   rdoMultiExtended.Text = "Multi-Extended";
   rdoMultiExtended.Tag = SelectionMode.MultiExtended;
   rdoMultiExtended.Location = new Point(10, rdoMultiSimple.Bottom);
   rdoMultiExtended.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);
   xSize = (int)(Font.Height * .75) * rdoMultiExtended.Text.Length;
   ySize = ((int)rdoMultiOne.Height * 3) + 20;
   grpMulti.Size = new Size(xSize, ySize);
   Panel pnlTop = new Panel();
   pnlTop.Parent = this;
   pnlTop.Location = new Point(lb.Left, grpMulti.Bottom + 10);
   pnlTop.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
   Label lblTop = new Label();
   lblTop.Parent = pnlTop;
   lblTop.Text = "TopIndex: ";
   xSize = ((int)(Font.Height * .5) * lblTop.Text.Length);
   lblTop.Size = new Size(xSize, Font.Height + 10);
   txtTop = new TextBox();
   txtTop.Parent = pnlTop;
   txtTop.Location = new Point(lblTop.Right, lblTop.Top);
   txtTop.Text = lb.TopIndex.ToString();
   txtTop.Size = new Size((int)(Font.Height * .75) * 3, 
               Font.Height + 10);
   btnTop = new Button();
   btnTop.Parent = pnlTop;
   btnTop.Text = "Update";
   btnTop.Location = new Point(txtTop.Right + 10, txtTop.Top);
   btnTop.Click += new System.EventHandler(btnTop_Click);
   lb.Items.Add("12345");
     lb.Items.Add("67890");      
     lb.Items.Add("7890");      
     lb.Items.Add("890");            
 } 
 static void Main() 
 {
   Application.Run(new ListBoxSelectionMode());
 }
 private void rdoMulti_CheckedChanged(object sender, EventArgs e)
 {
   RadioButton rdo = (RadioButton)sender;
   lb.SelectionMode = (SelectionMode)rdo.Tag;
 }
 private void btnTop_Click(object sender, EventArgs e)
 {
   txtTop.Text = lb.TopIndex.ToString();
 }

}

 </source>


ListBox.TopIndex

<source lang="csharp">

using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {

   private System.Windows.Forms.GroupBox GroupBox1;
   private System.Windows.Forms.Label Label4;
   private System.Windows.Forms.Label Label1;
   private System.Windows.Forms.PictureBox pic;
   private System.Windows.Forms.TextBox txt;
   private System.Windows.Forms.Button cmd;
   private System.Windows.Forms.Label Label2;
   private System.Windows.Forms.Label Label3;
   private System.Windows.Forms.ListBox eventLogList;
   public Form1() {
       InitializeComponent();
   }
   private void Log(String data)
   {
       eventLogList.Items.Add(data);
       int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
       eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
   }
   private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
   {
       Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
   }
   private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
   {
       Log("Key Press: " + e.KeyChar.ToString());
   }
   private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
   {
       Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
   }
   private void txt_TextChanged(object sender, System.EventArgs e)
   {
       Log("Changed: " + " Text is: " + txt.Text);
   }
   private void pic_MouseEnter(object sender, System.EventArgs e)
   {
       Log("Mouse Enter");
   }
   private void pic_MouseHover(object sender, System.EventArgs e)
   {
       Log("Mouse Hover");
   }
   private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
   {
       Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
   }
   private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
   {
       Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
   }
   private void pic_Click(object sender, System.EventArgs e)
   {
       Log("Click");
   }
   private void pic_DoubleClick(object sender, System.EventArgs e)
   {
       Log("Double Click");
   }
   private void pic_MouseLeave(object sender, System.EventArgs e)
   {
       Log("Mouse Leave");
   }
   private void InitializeComponent()
   {
       this.GroupBox1 = new System.Windows.Forms.GroupBox();
       this.Label4 = new System.Windows.Forms.Label();
       this.Label1 = new System.Windows.Forms.Label();
       this.pic = new System.Windows.Forms.PictureBox();
       this.txt = new System.Windows.Forms.TextBox();
       this.cmd = new System.Windows.Forms.Button();
       this.Label2 = new System.Windows.Forms.Label();
       this.Label3 = new System.Windows.Forms.Label();
       this.eventLogList = new System.Windows.Forms.ListBox();
       this.GroupBox1.SuspendLayout();
       ((System.ruponentModel.ISupportInitialize)(this.pic)).BeginInit();
       this.SuspendLayout();
       // 
       // GroupBox1
       // 
       this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                   | System.Windows.Forms.AnchorStyles.Right)));
       this.GroupBox1.Controls.Add(this.Label4);
       this.GroupBox1.Controls.Add(this.Label1);
       this.GroupBox1.Controls.Add(this.pic);
       this.GroupBox1.Controls.Add(this.txt);
       this.GroupBox1.Controls.Add(this.cmd);
       this.GroupBox1.Controls.Add(this.Label2);
       this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.GroupBox1.Location = new System.Drawing.Point(7, 0);
       this.GroupBox1.Name = "GroupBox1";
       this.GroupBox1.Size = new System.Drawing.Size(384, 148);
       this.GroupBox1.TabIndex = 12;
       this.GroupBox1.TabStop = false;
       // 
       // Label4
       // 
       this.Label4.Location = new System.Drawing.Point(92, 108);
       this.Label4.Name = "Label4";
       this.Label4.Size = new System.Drawing.Size(56, 16);
       this.Label4.TabIndex = 5;
       this.Label4.Text = "And here:";
       // 
       // Label1
       // 
       this.Label1.Location = new System.Drawing.Point(6, 24);
       this.Label1.Name = "Label1";
       this.Label1.Size = new System.Drawing.Size(144, 16);
       this.Label1.TabIndex = 2;
       this.Label1.Text = "Test keyboard events here:";
       // 
       // pic
       // 
       this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
       this.pic.Location = new System.Drawing.Point(156, 48);
       this.pic.Name = "pic";
       this.pic.Size = new System.Drawing.Size(192, 48);
       this.pic.TabIndex = 3;
       this.pic.TabStop = false;
       this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
       this.pic.Click += new System.EventHandler(this.pic_Click);
       this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
       this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
       this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
       this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
       // 
       // txt
       // 
       this.txt.Location = new System.Drawing.Point(156, 20);
       this.txt.Name = "txt";
       this.txt.Size = new System.Drawing.Size(192, 21);
       this.txt.TabIndex = 1;
       this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
       this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
       this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
       this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
       // 
       // cmd
       // 
       this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.cmd.Location = new System.Drawing.Point(156, 100);
       this.cmd.Name = "cmd";
       this.cmd.Size = new System.Drawing.Size(88, 28);
       this.cmd.TabIndex = 4;
       this.cmd.Text = "Button1";
       this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
       this.cmd.Click += new System.EventHandler(this.pic_Click);
       this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
       this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
       this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
       this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
       // 
       // Label2
       // 
       this.Label2.Location = new System.Drawing.Point(20, 52);
       this.Label2.Name = "Label2";
       this.Label2.Size = new System.Drawing.Size(128, 16);
       this.Label2.TabIndex = 2;
       this.Label2.Text = "Test mouse events here:";
       // 
       // Label3
       // 
       this.Label3.Location = new System.Drawing.Point(23, 100);
       this.Label3.Name = "Label3";
       this.Label3.Size = new System.Drawing.Size(64, 24);
       this.Label3.TabIndex = 11;
       this.Label3.Text = "Label3";
       // 
       // eventLogList
       // 
       this.eventLogList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                   | System.Windows.Forms.AnchorStyles.Left)
                   | System.Windows.Forms.AnchorStyles.Right)));
       this.eventLogList.FormattingEnabled = true;
       this.eventLogList.IntegralHeight = false;
       this.eventLogList.Location = new System.Drawing.Point(7, 156);
       this.eventLogList.Name = "eventLogList";
       this.eventLogList.Size = new System.Drawing.Size(384, 212);
       this.eventLogList.TabIndex = 10;
       // 
       // Form1
       // 
       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
       this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
       this.ClientSize = new System.Drawing.Size(399, 374);
       this.Controls.Add(this.GroupBox1);
       this.Controls.Add(this.Label3);
       this.Controls.Add(this.eventLogList);
       this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
       this.Name = "Form1";
       this.Text = "Event Tracker";
       this.GroupBox1.ResumeLayout(false);
       this.GroupBox1.PerformLayout();
       ((System.ruponentModel.ISupportInitialize)(this.pic)).EndInit();
       this.ResumeLayout(false);
   }
   [STAThread]
   static void Main()
   {
       Application.EnableVisualStyles();
       Application.Run(new Form1());
   }

}



 </source>


ListBox.ValueMemberChanged

<source lang="csharp">

using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.ruponentModel.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 Dispose( bool disposing )
 {
   if( disposing )
   {
     if (components != null) 
     {
       components.Dispose();
     }
   }
   base.Dispose( disposing );
 }
 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(16, 8);
   this.lb.Name = "lb";
   this.lb.Size = new System.Drawing.Size(232, 212);
   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(16, 240);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.TabIndex = 1;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "DataSource";
   // 
   // rbEmployees
   // 
   this.rbEmployees.Location = new System.Drawing.Point(24, 56);
   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(24, 32);
   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(5, 13);
   this.ClientSize = new System.Drawing.Size(264, 389);
   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);
 }

}

 </source>