C# Source Code: Brief demonstration of how to use data binding to populate a control
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Brief demonstration of how to use data binding to populate a control
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, July 30, 2004
Hits:
1080
Category:
Windows Forms/GUI/Controls
Article:
Below is a simple windows form project when demonstrates how to use databinding to populate a listbox control. using System; using System.Windows.Forms ; using System.Drawing ; using System.Collections ; namespace VBusers.WinForms.DataBinding { ///
/// Simple Form containing a listbox and a textbox. The /// listbox has been bound to an array list containing several User objects. ///
public class frmDataBindingExample:Form { private System.Windows.Forms.TextBox txtSelectedUser; private System.Windows.Forms.Label lblSelected; private System.Windows.Forms.ListBox lstUsers; public frmDataBindingExample() { // // Required for Windows Form Designer support // InitializeComponent(); } [STAThread] static void Main() { Application.Run(new frmDataBindingExample()) ; } private void InitializeComponent() { this.lstUsers = new System.Windows.Forms.ListBox(); this.txtSelectedUser = new System.Windows.Forms.TextBox(); this.lblSelected = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lstUsers // this.lstUsers.Location = new System.Drawing.Point(8, 6); this.lstUsers.Name = "lstUsers"; this.lstUsers.Size = new System.Drawing.Size(274, 199); this.lstUsers.TabIndex = 0; // // txtSelectedUser // this.txtSelectedUser.Location = new System.Drawing.Point(84, 216); this.txtSelectedUser.Name = "txtSelectedUser"; this.txtSelectedUser.Size = new System.Drawing.Size(198, 20); this.txtSelectedUser.TabIndex = 1; this.txtSelectedUser.Text = ""; // // lblSelected // this.lblSelected.Location = new System.Drawing.Point(8, 218); this.lblSelected.Name = "lblSelected"; this.lblSelected.Size = new System.Drawing.Size(98, 18); this.lblSelected.TabIndex = 2; this.lblSelected.Text = "Selected User:"; // // frmDataBindingExample // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 245); this.Controls.Add(this.txtSelectedUser); this.Controls.Add(this.lstUsers); this.Controls.Add(this.lblSelected); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "frmDataBindingExample"; this.Text = "Data Binding Example"; this.Load += new System.EventHandler(this.frmDataBindingExample_Load); this.ResumeLayout(false); } private void frmDataBindingExample_Load(object sender, System.EventArgs e) { // DisplayMember is used to display just the long name of each state. ArrayList UserNames = new ArrayList() ; UserNames.Add(new UserName("Andrew", "Baker")); UserNames.Add(new UserName("Shannon", "Baker")) ; UserNames.Add(new UserName("Seymore", "Butts")); UserNames.Add(new UserName("Ivor", "Bigun")) ; UserNames.Add(new UserName("Willy", "McWinky")); lstUsers.SelectedValueChanged += new EventHandler(lstUsers_SelectedValueChanged); // Populate the list box by setting it's DataSource lstUsers.DataSource = UserNames; // Set the listbox to display the FirstName property. lstUsers.DisplayMember = "FirstName"; // Set the listbox to use the FullName as the value property. lstUsers.ValueMember = "FullName" ; } private void lstUsers_SelectedValueChanged(object sender, EventArgs e) { if (lstUsers.SelectedIndex != -1) { // Return the Full Name into the textbox txtSelectedUser.Text = lstUsers.SelectedValue.ToString(); } } } ///
/// Simple Class to hold a user name ///
public class UserName { private string _firstName ; private string _lastName ; public UserName(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; } public string FirstName { get { return _firstName; } } public string FullName { get { return _firstName + " " + _lastName; } } public string LastName { get { return _lastName ; } } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet