Csharp/C Sharp/GUI Windows Form/ListBox Data Binding

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

Data ListBox Binding 3

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

}


      </source>

<A href="http://www.nfex.ru/Code/CSharpDownload/ADO.NETBinding.zip">ADO.NETBinding.zip( 76 k)</a>


Editable Binding

<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.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.ruboBox cboModelName;
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.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.ruboBox();
           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.ruboBoxStyle.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());
       }
   }

}


      </source>

<A href="http://www.nfex.ru/Code/CSharpDownload/ADO.NETBinding.zip">ADO.NETBinding.zip( 76 k)</a>


ListBox: Binding Unusual Properties

<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 {

   /// <summary>
   /// Summary description for BindingUnusualProperties.
   /// </summary>
   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;
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public BindingUnusualProperties()
       {
           //
           // 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.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();
           // 
           // Label2
           // 
           this.Label2.Location = new System.Drawing.Point(210, 13);
           this.Label2.Name = "Label2";
           this.Label2.Size = new System.Drawing.Size(140, 12);
           this.Label2.TabIndex = 9;
           this.Label2.Text = "Choose a Font:";
           // 
           // Label1
           // 
           this.Label1.Location = new System.Drawing.Point(14, 13);
           this.Label1.Name = "Label1";
           this.Label1.Size = new System.Drawing.Size(140, 12);
           this.Label1.TabIndex = 8;
           this.Label1.Text = "Choose a Color:";
           // 
           // lstFonts
           // 
           this.lstFonts.Location = new System.Drawing.Point(210, 29);
           this.lstFonts.Name = "lstFonts";
           this.lstFonts.Size = new System.Drawing.Size(180, 134);
           this.lstFonts.TabIndex = 7;
           // 
           // lblSampleText
           // 
           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.";
           // 
           // lstColors
           // 
           this.lstColors.Location = new System.Drawing.Point(14, 29);
           this.lstColors.Name = "lstColors";
           this.lstColors.Size = new System.Drawing.Size(176, 134);
           this.lstColors.TabIndex = 5;
           // 
           // BindingUnusualProperties
           // 
           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);
       }
       #endregion
       private void BindingUnusualProperties_Load(object sender, System.EventArgs e)
       {
           // These are our final data sources: two ArrayList objects.
           ArrayList fontObjList = new ArrayList();
           ArrayList colorObjList = new ArrayList();
           // The InstalledFonts collection allows us to enumerate installed fonts.
           // Each FontFamily needs to be converted to a genuine Font object
           // before it is suitable for data binding to the Control.Font property.
           InstalledFontCollection InstalledFonts = new InstalledFontCollection();
           foreach (FontFamily family in InstalledFonts.Families)
           {
               try
               {
                   fontObjList.Add(new Font(family, 12));
               }
               catch
               {
                   // We end up here if the font could not be created
                   // with the default style.
               }
           }
           // In order to retrieve the list of colors, we need to first retrieve
           // the strings for the KnownColor enumeration, and then convert each one
           // into a suitable color object.
           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>


Object List Binding

<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>


Object ListBox Data Binding 2

<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 ObjectListBinding2.
   /// </summary>
   public class ObjectListBinding2 : System.Windows.Forms.Form
   {
       internal System.Windows.Forms.ListBox lstCity;
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public ObjectListBinding2()
       {
           //
           // 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, 9);
           this.lstCity.Name = "lstCity";
           this.lstCity.Size = new System.Drawing.Size(248, 216);
           this.lstCity.TabIndex = 3;
           // 
           // ObjectListBinding2
           // 
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
           this.ClientSize = new System.Drawing.Size(260, 234);
           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 = "ObjectListBinding2";
           this.Text = "ObjectListBinding2";
           this.Load += new System.EventHandler(this.ObjectListBinding2_Load);
           this.ResumeLayout(false);
       }
       #endregion
       [STAThread]
       static void Main() 
       {
           Application.Run(new ObjectListBinding2());
       }
       private void ObjectListBinding2_Load(object sender, System.EventArgs e)
       {
           City2[] cityChoices = {new City2("Seattle", "U.S.A."), 
                                    new City2("New York", "U.S.A."), new City2("Tokyo", "Japan"), 
                                    new City2("Montreal", "Canada")};
           lstCity.DataSource = cityChoices;
       }
   
   }
   public class City2
   {
       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 City2(string name, string country)
       {
           this.Name = name;
           this.Country = country;
       }
       public override string ToString()
       {
           return Name + ", " + Country;
       }
   }

}


      </source>


Single Item Data Binding

<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>