CheckedListBox Demo 2

using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

public class CheckedListBoxDemo:Form{
CheckedListBox FavLangs;
GroupBox grpControls;
Button AddValue;
Button EditValue;
Button DeleteValue;
Button ShowValues;
TextBox OldValue;
TextBox NewValue;
Label OldCaption;
Label NewCaption;
CheckBox chkAll;

public CheckedListBoxDemo(){
grpControls=new GroupBox();
grpControls.Text=”CheckedListBox Demo”;

AddValue=new Button();
AddValue.Text=”&Add”;
AddValue.Click+=new EventHandler(Add_Click);

EditValue=new Button();
EditValue.Text=”&Edit”;
EditValue.Click+=new EventHandler(Edit_Click);

DeleteValue=new Button();
DeleteValue.Text=”&Delete”;
DeleteValue.Click+=new EventHandler(Delete_Click);

ShowValues=new Button();
ShowValues.Text=”&Show”;
//ShowValues.Click+=new EventHandler(ShowValues_Click);
ShowValues.Click+=new EventHandler(Checked_Changed);

OldValue=new TextBox();
OldValue.ReadOnly=true;
NewValue=new TextBox();

OldCaption=new Label();
OldCaption.Text=”Old Value:”;
NewCaption=new Label();
NewCaption.Text=”New Value:”;

chkAll=new CheckBox();
chkAll.Text=”Check/UnCheck All”;
chkAll.CheckedChanged+= new EventHandler(Checked_Changed);
chkAll.Width=175;

OldCaption.Location=new Point(15,15);
PositionControl(OldCaption,OldValue,true);
PositionControl(OldCaption,NewCaption,false);
PositionControl(OldValue,NewValue,false);
PositionControl(NewCaption,AddValue,false);
PositionControl(AddValue,EditValue,true);
PositionControl(EditValue,DeleteValue,true);
PositionControl(DeleteValue,ShowValues,true);
PositionControl(AddValue,chkAll,false);

grpControls.Controls.AddRange(new Control[]{OldCaption,OldValue,NewCaption,NewValue,AddValue,EditValue,DeleteValue,ShowValues,chkAll});
grpControls.Size=new Size(450,200);

FavLangs=new CheckedListBox();
FavLangs.Location=new Point(10,10);
FavLangs.SelectedIndexChanged+=new EventHandler(SelectedIndex_Changed);

grpControls.Location=new Point(FavLangs.Left+FavLangs.Width+20,FavLangs.Top);
this.Controls.AddRange(new Control[]{FavLangs,grpControls});
}

private void PositionControl(Control source,Control destination,bool CanPlaceHorizontal)
{
if(CanPlaceHorizontal){
destination.Location=new Point(source.Left+source.Width+20,source.Top);
}else{
destination.Location=new Point(source.Left,source.Top+source.Height+20);
}
}

private void Add_Click(object sender,EventArgs e){
((Button)sender).Text = “aaa”;

if(NewValue.Text.Trim()!=””){
FavLangs.Items.Add(NewValue.Text);
}else{
MessageBox.Show(“Enter a Value to Add”);
}
}

private void SelectedIndex_Changed(object sender,EventArgs e){
OldValue.Text=FavLangs.Items[FavLangs.SelectedIndex].ToString();
}

private void Edit_Click(object sender,EventArgs e){
if(FavLangs.SelectedIndex==-1){
MessageBox.Show(“Select a Item to Edit”);
} else{
if(NewValue.Text.Trim()!=””){
FavLangs.Items[FavLangs.SelectedIndex]=NewValue.Text;
}
else
{
MessageBox.Show(“Enter a Value to Edit”);
}
}
}

private void Delete_Click(object sender,EventArgs e)
{
if(FavLangs.SelectedIndex!=-1)
{
FavLangs.Items.RemoveAt(FavLangs.SelectedIndex);
}
else
{
MessageBox.Show(“Select a Item to Delete”);
}
}

private void ShowValues_Click(object sender,EventArgs e){
string SelectedValues=”The following value(s) are Selected:
” + new String('-',48) + ”
“;
for(int i=0;i

ListBox: font and image


/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyListBox
{
///

/// Summary description for MyListBox.
///

public class MyListBox : System.Windows.Forms.Form
{
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ListBox VARIABLE;
private System.Windows.Forms.ListBox FIXED;
private System.Windows.Forms.ListBox SIMPLE;
private System.Windows.Forms.ListBox MULTI_COLUMN;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ListBox MULTI_SELECTION;
private System.Windows.Forms.Label label5;
static int delta = 5 ;
string[] ListItems = new string[7];

public MyListBox()
{
ListItems[0] = “Apples”;
ListItems[1] = “Oranges”;
ListItems[2] = “Mangoes”;
ListItems[3] = “PineApple”;
ListItems[4] = “Strawbverries”;
ListItems[5] = “Bananas”;
ListItems[6] = “GrapeFruit”;

//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///

/// Clean up any resources being used.
///

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

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.VARIABLE = new System.Windows.Forms.ListBox();
this.FIXED = new System.Windows.Forms.ListBox();
this.SIMPLE = new System.Windows.Forms.ListBox();
this.MULTI_COLUMN = new System.Windows.Forms.ListBox();
this.label4 = new System.Windows.Forms.Label();
this.MULTI_SELECTION = new System.Windows.Forms.ListBox();
this.label5 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font(“Microsoft Sans Serif”, 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(208, 24);
this.label1.Name = “label1”;
this.label1.Size = new System.Drawing.Size(192, 23);
this.label1.TabIndex = 1;
this.label1.Text = “Fixed Owner Draw List Box”;
//
// label2
//
this.label2.Font = new System.Drawing.Font(“Microsoft Sans Serif”, 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(24, 24);
this.label2.Name = “label2”;
this.label2.Size = new System.Drawing.Size(168, 23);
this.label2.TabIndex = 2;
this.label2.Text = “Simple List Box”;
//
// label3
//
this.label3.Font = new System.Drawing.Font(“Microsoft Sans Serif”, 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label3.Location = new System.Drawing.Point(424, 24);
this.label3.Name = “label3”;
this.label3.Size = new System.Drawing.Size(208, 16);
this.label3.TabIndex = 1;
this.label3.Text = “Variable Owner Draw List Box”;
//
// VARIABLE
//
this.VARIABLE.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.VARIABLE.Location = new System.Drawing.Point(424, 56);
this.VARIABLE.Name = “VARIABLE”;
this.VARIABLE.Size = new System.Drawing.Size(200, 240);
this.VARIABLE.TabIndex = 5;
this.VARIABLE.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.VARIABLE_MeasureItem);
this.VARIABLE.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.VARIABLE_DrawItem);
this.VARIABLE.SelectedIndexChanged += new System.EventHandler(this.VARIABLE_SelectedIndexChanged);
//
// FIXED
//
this.FIXED.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.FIXED.Location = new System.Drawing.Point(208, 56);
this.FIXED.Name = “FIXED”;
this.FIXED.Size = new System.Drawing.Size(208, 238);
this.FIXED.TabIndex = 4;
this.FIXED.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.FIXED_DrawItem);
this.FIXED.SelectedIndexChanged += new System.EventHandler(this.FIXED_SelectedIndexChanged);
//
// SIMPLE
//
this.SIMPLE.Location = new System.Drawing.Point(24, 56);
this.SIMPLE.Name = “SIMPLE”;
this.SIMPLE.Size = new System.Drawing.Size(176, 238);
this.SIMPLE.TabIndex = 3;
this.SIMPLE.SelectedIndexChanged += new System.EventHandler(this.SIMPLE_SelectedIndexChanged);
//
// MULTI_COLUMN
//
this.MULTI_COLUMN.Location = new System.Drawing.Point(24, 344);
this.MULTI_COLUMN.MultiColumn = true;
this.MULTI_COLUMN.Name = “MULTI_COLUMN”;
this.MULTI_COLUMN.Size = new System.Drawing.Size(224, 69);
this.MULTI_COLUMN.TabIndex = 6;
this.MULTI_COLUMN.SelectedIndexChanged += new System.EventHandler(this.MULTI_COLUMN_SelectedIndexChanged);
//
// label4
//
this.label4.Font = new System.Drawing.Font(“Microsoft Sans Serif”, 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label4.Location = new System.Drawing.Point(24, 320);
this.label4.Name = “label4”;
this.label4.Size = new System.Drawing.Size(168, 24);
this.label4.TabIndex = 7;
this.label4.Text = “Multi Column ListBox”;
//
// MULTI_SELECTION
//
this.MULTI_SELECTION.Location = new System.Drawing.Point(296, 344);
this.MULTI_SELECTION.MultiColumn = true;
this.MULTI_SELECTION.Name = “MULTI_SELECTION”;
this.MULTI_SELECTION.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
this.MULTI_SELECTION.Size = new System.Drawing.Size(200, 147);
this.MULTI_SELECTION.TabIndex = 6;
this.MULTI_SELECTION.SelectedIndexChanged += new System.EventHandler(this.MULTI_SELECTION_SelectedIndexChanged);
//
// label5
//
this.label5.Font = new System.Drawing.Font(“Microsoft Sans Serif”, 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label5.Location = new System.Drawing.Point(296, 320);
this.label5.Name = “label5”;
this.label5.Size = new System.Drawing.Size(208, 24);
this.label5.TabIndex = 7;
this.label5.Text = “Multi Selection ListBox”;
//
// MyListBox
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(640, 493);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label4,
this.MULTI_COLUMN,
this.VARIABLE,
this.FIXED,
this.SIMPLE,
this.label2,
this.label1,
this.label3,
this.MULTI_SELECTION,
this.label5});
this.Name = “MyListBox”;
this.Text = “ListBox Style”;
this.Load += new System.EventHandler(this.MyListBox_Load);
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///

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

private void MyListBox_Load(object sender, System.EventArgs e)
{
// Set the height of the ListBox with OwnerDrawFixed property
FIXED.ItemHeight = 35 ;

// Populate all the ListBoxes
for ( int i=0; i < ListItems.Length; i++) { MULTI_SELECTION.Items.Add(ListItems[i]); MULTI_COLUMN.Items.Add(ListItems[i]); SIMPLE.Items.Add(ListItems[i]); FIXED.Items.Add(ListItems[i]); VARIABLE.Items.Add(ListItems[i]); } } private void FIXED_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { //Select the Icon that you want to display Icon i = new Icon("TICK.ico"); // Get the Bounding rectangle Rectangle rc = new Rectangle(e.Bounds.X + delta , e.Bounds.Y + delta , e.Bounds.Width-10, e.Bounds.Height-delta); Console.WriteLine(e.State.ToString()); // Setup the stringformatting object StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center ; // Get the item text FIXED = (ListBox)sender ; string str = (string)FIXED.Items[e.Index]; // Draw the rectangle e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black) , 2) , rc); e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc); // Check if the item is selected if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { // Paint the item that if not selected e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc); e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf); e.DrawFocusRectangle(); } else { // Paint the item accordingly if it is selected e.DrawFocusRectangle(); e.Graphics.FillRectangle(new SolidBrush(Color.LightYellow) , rc); e.Graphics.DrawIcon(i, e.Bounds.X , e.Bounds.Y+5); e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf); } } private void VARIABLE_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { //Select the Icon that you want to display Icon i = new Icon("TICK.ico"); // Get the Bounding rectangle Rectangle rc = new Rectangle(e.Bounds.X + delta , e.Bounds.Y + delta , e.Bounds.Width-10, e.Bounds.Height-delta); Console.WriteLine(e.State.ToString()); // Setup the stringformatting object StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center ; // UnBox the sender VARIABLE = (ListBox)sender ; // Get the item text string str = (string)VARIABLE.Items[e.Index]; // Draw the rectangle e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black) , 2) , rc); // Fill the rectangle with white background. Default Item not selected e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc); // Check if the item is selected if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { // Paint the item that if not selected e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc); e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf); e.DrawFocusRectangle(); } else { // Paint the item accordingly if it is selected e.DrawFocusRectangle(); e.Graphics.FillRectangle(new SolidBrush(Color.LightCyan) , rc); e.Graphics.DrawIcon(i, e.Bounds.X , e.Bounds.Y+5); e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf); } } private void VARIABLE_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { // For every second item in the VariableListBox, set the itemheight to 55 if ( e.Index %2 == 0 ) { e.ItemHeight = 55 ; } else { // For all other items set the items to 33 e.ItemHeight = 33 ; } } private void SIMPLE_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender SIMPLE = (ListBox)sender ; // Get the position of the item selected int posSelected = SIMPLE.SelectedIndex ; // Get the item text string str = (string)SIMPLE.Items[posSelected]; MessageBox.Show("ITEM SELECTED FROM SIMPLE LIST BOX = " + str , "Simple ListBox"); } private void FIXED_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender FIXED = (ListBox)sender ; // Get the position of the item selected int posSelected = FIXED.SelectedIndex ; // Get the item text string str = (string)FIXED.Items[posSelected]; MessageBox.Show("ITEM SELECTED FROM FIXED LIST BOX = " + str , "Fixed Height ListBox"); } private void VARIABLE_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender VARIABLE = (ListBox)sender ; // Get the position of the item selected int posSelected = VARIABLE.SelectedIndex ; // Get the item text string str = (string) VARIABLE.Items[posSelected]; MessageBox.Show("ITEM SELECTED FROM VARIABLE LIST BOX = " + str , "Variable Height ListBox"); } private void MULTI_COLUMN_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender MULTI_COLUMN = (ListBox)sender ; // Get the position of the item selected int posSelected = MULTI_COLUMN.SelectedIndex ; // Get the item text string str = (string) MULTI_COLUMN.Items[posSelected]; MessageBox.Show("ITEM SELECTED FROM MULTI_COLUM LIST BOX = " + str , "Multi Column"); } private void MULTI_SELECTION_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender MULTI_SELECTION = (ListBox)sender ; string str = "" ; // Get the list of selected index's for ( int i=0; i < MULTI_SELECTION.SelectedIndices.Count; i++) { // Get the index int posSelected = MULTI_SELECTION.SelectedIndices[i]; // Get the item text at the index str = str + (string)MULTI_SELECTION.Items[posSelected] + "," ; } MessageBox.Show("ITEM SELECTED FROM MULTI_SELECTION LIST BOX = " + str , "Multi Selection"); } } } MyListBox.zip( 49 k)[/csharp]

Form with list, button


   

/*
C# Programming Tips &amp; 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.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Modeless
{
  /// <summary>
  /// Summary description for MainForm.
  /// </summary>
  public class MainFormDemo : System.Windows.Forms.Form
  {
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    AddItemForm frmAdd;

    public MainFormDemo()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      frmAdd = new AddItemForm (this);

    }

    /// <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.listBox1 = new System.Windows.Forms.ListBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.button3 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // listBox1
      // 
      this.listBox1.Location = new System.Drawing.Point(42, 16);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(208, 199);
      this.listBox1.TabIndex = 0;
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(17, 240);
      this.button1.Name = "button1";
      this.button1.TabIndex = 1;
      this.button1.Text = "Add Item";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(109, 240);
      this.button2.Name = "button2";
      this.button2.TabIndex = 2;
      this.button2.Text = "Delete Item";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // button3
      // 
      this.button3.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      this.button3.Location = new System.Drawing.Point(201, 240);
      this.button3.Name = "button3";
      this.button3.TabIndex = 3;
      this.button3.Text = "Close";
      this.button3.Click += new System.EventHandler(this.button3_Click);
      // 
      // MainForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.CancelButton = this.button3;
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button3,
                                      this.button2,
                                      this.button1,
                                      this.listBox1});
      this.Name = "MainForm";
      this.Text = "MainForm";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new MainFormDemo());
    }


    private void button1_Click(object sender, System.EventArgs e)
    {
      if (frmAdd.IsDisposed == true)
        frmAdd = new AddItemForm (this);
      frmAdd.Show ();
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
      if (listBox1.SelectedIndex < 0)
        return;
      object obj = listBox1.Items&#91;listBox1.SelectedIndex&#93;;
      listBox1.Items.Remove (obj);
    }

    private void button3_Click(object sender, System.EventArgs e)
    {
      Application.Exit ();
    }

    public string AddItemToList (string strAdd)
    {
      if (strAdd == "")
        return ("");
      if (listBox1.FindString (strAdd, -1) < 0)
      {
        listBox1.Items.Add (strAdd);
        return ("");
      }
      MessageBox.Show (""" + strAdd + "" is already in the list box", "Duplicate");
      return (strAdd);
    }
  }
  /// <summary>
  /// Summary description for AddItemForm.
  /// </summary>
  public class AddItemForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    MainFormDemo parent;
    public AddItemForm(MainFormDemo parent)
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      this.parent = parent;
    }

    /// <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.textBox1 = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(28, 8);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(224, 20);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(40, 48);
      this.button1.Name = "button1";
      this.button1.TabIndex = 2;
      this.button1.Text = "Add";
      this.button1.Click += new System.EventHandler(this.button1_Click_1);
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(184, 48);
      this.button2.Name = "button2";
      this.button2.TabIndex = 3;
      this.button2.Text = "Close";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // AddItemForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(280, 77);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button2,
                                      this.button1,
                                      this.textBox1});
      this.Name = "AddItemForm";
      this.Text = "AddItem";
      this.ResumeLayout(false);

    }
    #endregion

    private void button1_Click_1(object sender, System.EventArgs e)
    {
      textBox1.Text = parent.AddItemToList (textBox1.Text);
//      textBox1.Text = "";
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
      this.Dispose ();
      this.Close ();
    }
  }

}



           
          


Set TopIndex to auto scroll ListBox


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
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.ComponentModel.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.ComponentModel.ISupportInitialize)(this.pic)).EndInit();
        this.ResumeLayout(false);

    }

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

}


           
          


List Box click event


   

using System;
using System.Collections.Generic;
using System.ComponentModel;
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());
  }

}



           
          


Add Object to ListBox


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
  }
}

           
          


ListBox selected Item changed event


   

using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
  }
}