using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DataBindingFundamentalsSample { public partial class ComplexBindingListBoxForm : Form { // Create strongly-typed list data source List raceCarDrivers = new List(); public ComplexBindingListBoxForm() { InitializeComponent(); // Populate list data source with data items this.raceCarDrivers.Add(new RaceCarDriver("M. Schumacher", 500)); this.raceCarDrivers.Add(new RaceCarDriver("A. Senna", 1000)); this.raceCarDrivers.Add(new RaceCarDriver("A. Prost", 400)); // Bind the Name and Wins properties to the name and Wins text boxes this.nameTextBox.DataBindings.Add("Text", this.raceCarDrivers, "Name"); this.winsTextBox.DataBindings.Add("Text", this.raceCarDrivers, "Wins"); // Complex bind list box to RaceCarDrivers list data source this.raceCarDriversListBox.DataSource = this.raceCarDrivers; // Specify the property whose value will appear in the listbox // for each item in the list data source this.raceCarDriversListBox.DisplayMember = "Name"; // Setup currency information handling RefreshItems(); } BindingManagerBase BindingManager { get { return this.BindingContext[this.raceCarDrivers]; } } private void moveFirstButton_Click(object sender, EventArgs e) { this.BindingManager.Position = 0; RefreshItems(); } private void movePreviousButton_Click(object sender, EventArgs e) { // No need to worry about being < 0 --this.BindingManager.Position; RefreshItems(); } private void moveNextButton_Click(object sender, EventArgs e) { // No need to worry about being > this.BindingManager.Count ++this.BindingManager.Position; RefreshItems(); } private void moveLastButton_Click(object sender, EventArgs e) { this.BindingManager.Position = this.BindingManager.Count - 1; RefreshItems(); } void RefreshItems() { int count = this.BindingManager.Count; int position = this.BindingManager.Position + 1; // Update count and position text this.countLabel.Text = count.ToString(); this.positionLabel.Text = position.ToString(); // Enable/Disable move buttons this.moveFirstButton.Enabled = (position > 1); this.movePreviousButton.Enabled = (position > 1); this.moveNextButton.Enabled = (position < count); this.moveLastButton.Enabled = (position < count); } } }