Clear all items in a 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 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());
    }

}



           
          


Remove item if one is selected from 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 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());
    }

}


           
          


Add new item to ListBox (text from TextBox)


   


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 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());
    }

}


           
          


Editable Binding



   

/*
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.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ADO.NET_Binding
{
    /// <summary>
    /// Summary description for EditableBinding.
    /// </summary>
    public class EditableBinding : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Label Label4;
        internal System.Windows.Forms.GroupBox GroupBox1;
        internal System.Windows.Forms.Label Label3;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.TextBox txtModelName;
        internal System.Windows.Forms.TextBox txtDescription;
        internal System.Windows.Forms.TextBox txtUnitCost;
        internal System.Windows.Forms.TextBox txtModelNumber;
        internal System.Windows.Forms.ComboBox cboModelName;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public EditableBinding()
        {
            //
            // 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.Label4 = new System.Windows.Forms.Label();
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.Label3 = new System.Windows.Forms.Label();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.txtModelName = new System.Windows.Forms.TextBox();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.txtUnitCost = new System.Windows.Forms.TextBox();
            this.txtModelNumber = new System.Windows.Forms.TextBox();
            this.cboModelName = new System.Windows.Forms.ComboBox();
            this.GroupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Label4
            // 
            this.Label4.Location = new System.Drawing.Point(16, 17);
            this.Label4.Name = "Label4";
            this.Label4.Size = new System.Drawing.Size(88, 16);
            this.Label4.TabIndex = 20;
            this.Label4.Text = "Select a Record:";
            // 
            // GroupBox1
            // 
            this.GroupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.Label3,
                                                                                    this.Label2,
                                                                                    this.Label1,
                                                                                    this.txtModelName,
                                                                                    this.txtDescription,
                                                                                    this.txtUnitCost,
                                                                                    this.txtModelNumber});
            this.GroupBox1.Location = new System.Drawing.Point(16, 53);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(400, 252);
            this.GroupBox1.TabIndex = 19;
            this.GroupBox1.TabStop = false;
            // 
            // Label3
            // 
            this.Label3.Location = new System.Drawing.Point(220, 56);
            this.Label3.Name = "Label3";
            this.Label3.Size = new System.Drawing.Size(36, 16);
            this.Label3.TabIndex = 18;
            this.Label3.Text = "Cost:";
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(16, 56);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(52, 16);
            this.Label2.TabIndex = 17;
            this.Label2.Text = "Model:";
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(16, 28);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(52, 16);
            this.Label1.TabIndex = 16;
            this.Label1.Text = "Name:";
            // 
            // txtModelName
            // 
            this.txtModelName.Location = new System.Drawing.Point(68, 24);
            this.txtModelName.Name = "txtModelName";
            this.txtModelName.Size = new System.Drawing.Size(316, 21);
            this.txtModelName.TabIndex = 15;
            this.txtModelName.Text = "";
            // 
            // txtDescription
            // 
            this.txtDescription.Location = new System.Drawing.Point(12, 92);
            this.txtDescription.Multiline = true;
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(372, 116);
            this.txtDescription.TabIndex = 14;
            this.txtDescription.Text = "";
            // 
            // txtUnitCost
            // 
            this.txtUnitCost.Location = new System.Drawing.Point(256, 52);
            this.txtUnitCost.Name = "txtUnitCost";
            this.txtUnitCost.Size = new System.Drawing.Size(128, 21);
            this.txtUnitCost.TabIndex = 13;
            this.txtUnitCost.Text = "";
            // 
            // txtModelNumber
            // 
            this.txtModelNumber.Location = new System.Drawing.Point(68, 52);
            this.txtModelNumber.Name = "txtModelNumber";
            this.txtModelNumber.Size = new System.Drawing.Size(136, 21);
            this.txtModelNumber.TabIndex = 12;
            this.txtModelNumber.Text = "";
            // 
            // cboModelName
            // 
            this.cboModelName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboModelName.Location = new System.Drawing.Point(108, 13);
            this.cboModelName.Name = "cboModelName";
            this.cboModelName.Size = new System.Drawing.Size(308, 21);
            this.cboModelName.TabIndex = 18;
            // 
            // EditableBinding
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(432, 318);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Label4,
                                                                          this.GroupBox1,
                                                                          this.cboModelName});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "EditableBinding";
            this.Text = "EditableBinding";
            this.Load += new System.EventHandler(this.EditableBinding_Load);
            this.GroupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void EditableBinding_Load(object sender, System.EventArgs e)
        {
            DataSet dsStore = new DataSet();
            dsStore.ReadXmlSchema(Application.StartupPath + "store.xsd");
            dsStore.ReadXml(Application.StartupPath + "store.xml");

            cboModelName.DataSource = dsStore.Tables["Products"];
            cboModelName.DisplayMember = "ModelName";

            txtModelName.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelName");
            txtModelNumber.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelNumber");
            txtUnitCost.DataBindings.Add("Text", dsStore.Tables["Products"], "UnitCost");
            txtDescription.DataBindings.Add("Text", dsStore.Tables["Products"], "Description");

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


           
          


ADO.NETBinding.zip( 76 k)

Data ListBox Binding 3



   

/*
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.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ADO.NET_Binding
{
    /// <summary>
    /// Summary description for DataListBinding.
    /// </summary>
    public class DataListBinding : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ListBox lstName;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DataListBinding()
        {
            //
            // 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.lstName = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // lstName
            // 
            this.lstName.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lstName.IntegralHeight = false;
            this.lstName.Location = new System.Drawing.Point(8, 8);
            this.lstName.Name = "lstName";
            this.lstName.Size = new System.Drawing.Size(232, 184);
            this.lstName.TabIndex = 1;
            // 
            // DataListBinding
            // 
            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.lstName});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "DataListBinding";
            this.Text = "DataListBinding";
            this.Load += new System.EventHandler(this.DataListBinding_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void DataListBinding_Load(object sender, System.EventArgs e)
        {
            DataSet dsStore = new DataSet();
            dsStore.ReadXmlSchema(Application.StartupPath + "store.xsd");
            dsStore.ReadXml(Application.StartupPath + "store.xml");

            lstName.DataSource = dsStore.Tables["Products"];
            lstName.DisplayMember = "ModelName";

        }

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


           
          


ADO.NETBinding.zip( 76 k)

Object List Binding


   

/*
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.ComponentModel;
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.ComponentModel.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;
        }
    }

}

           
          


Single Item Data Binding


   

/*
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.ComponentModel;
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.ComponentModel.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());
        }
    }
}