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 SimpleBindingAndListDataSourcesForm : Form { // Create strongly-typed list data source List raceCarDrivers = new List(); public SimpleBindingAndListDataSourcesForm() { 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"); // Setup currency information handling RefreshItems(); this.BindingManager.CurrentChanged += BindingManager_CurrentChanged; } void BindingManager_CurrentChanged(object sender, EventArgs e) { RefreshItems(); } BindingManagerBase BindingManager { get { return this.BindingContext[this.raceCarDrivers]; } } private void moveFirstButton_Click(object sender, EventArgs e) { this.BindingManager.Position = 0; } private void movePreviousButton_Click(object sender, EventArgs e) { // No need to worry about being < 0 --this.BindingManager.Position; } private void moveNextButton_Click(object sender, EventArgs e) { // No need to worry about being > 0 ++this.BindingManager.Position; } private void moveLastButton_Click(object sender, EventArgs e) { this.BindingManager.Position = this.BindingManager.Count; } 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); } } }