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 TheLandBeforeDataBindingForm : Form { RaceCarDriver raceCarDriver = new RaceCarDriver("M. Schumacher", 500); public TheLandBeforeDataBindingForm() { InitializeComponent(); // Copy initial RaceCarDriver state to text box controls this.nameTextBox.Text = this.raceCarDriver.Name; this.winsTextBox.Text = this.raceCarDriver.Wins.ToString(); // Detect changes to text box controls this.nameTextBox.TextChanged += this.nameTextBox_TextChanged; this.winsTextBox.TextChanged += this.winsTextBox_TextChanged; // NOTE: Uncomment the following (and the related bits in RaceCarDriver // to make the PropertyNameChanged property change notification // event model work. // Detect changes to RaceCarDriver object this.raceCarDriver.PropertyChanged += raceCarDriver_PropertyChanged; } void raceCarDriver_PropertyChanged(object sender, PropertyChangedEventArgs e) { switch( e.PropertyName ) { case "Name": this.nameTextBox.Text = this.raceCarDriver.Name; break; case "Wins": this.winsTextBox.Text = this.raceCarDriver.Wins.ToString(); break; } } private void nameTextBox_TextChanged(object sender, EventArgs e) { this.raceCarDriver.Name = this.nameTextBox.Text; } private void winsTextBox_TextChanged(object sender, EventArgs e) { this.raceCarDriver.Wins = int.Parse(this.winsTextBox.Text); } private void addWinButton_Click(object sender, EventArgs e) { // Causes the RaceCarDriver object's PropertyChanged event to fire, // which is used to keep the Wins text box up to date ++this.raceCarDriver.Wins; //// Don’t forget to update the Wins text box! //this.winsTextBox.Text = raceCarDriver.Wins.ToString(); } } }