Advertisement
Guest User

Example Program

a guest
Jun 7th, 2013
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1.     public partial class Form1 : Form
  2.     {
  3.         DataComponent dc;
  4.         public Form1()
  5.         {
  6.             InitializeComponent();
  7.             dc = new DataComponent();
  8.             checkBox1.CheckedChanged += (s, e) => { customControl1.Text = "3"; label1.Text = dc.BooleanVal.ToString(); };
  9.             button1.Click += (s, e) => { dc.BooleanVal = !dc.BooleanVal; label1.Text = dc.BooleanVal.ToString(); };
  10.         }
  11.         protected override void OnLoad(EventArgs e)
  12.         {
  13.             base.OnLoad(e);
  14.             checkBox1.DataBindings.Add("Checked", dc, "BooleanVal", false, DataSourceUpdateMode.OnPropertyChanged);
  15.            
  16.             customControl1.DataBindings.Add("BaseValue", dc, "FloatVal", false, DataSourceUpdateMode.OnPropertyChanged);            
  17.         }
  18.     }
  19.  
  20.     class CustomControl : TextBox, INotifyPropertyChanged
  21.     {
  22.         public override string Text
  23.         {
  24.             get
  25.             {
  26.                 return base.Text;
  27.             }
  28.             set
  29.             {
  30.                 base.Text = value;
  31.                 float s;
  32.                 if (!float.TryParse(value, out s))
  33.                     s = 0.0f;
  34.                 this.BaseValue = s;
  35.             }
  36.         }
  37.         public float BaseValue
  38.         {
  39.             get { return baseValue; }
  40.             set
  41.             {
  42.                 baseValue = value;
  43.                 this.OnPropertyChanged("BaseValue");
  44.             }
  45.         }
  46.         float baseValue;
  47.         public void OnPropertyChanged(string propertyName)
  48.         {
  49.             if (PropertyChanged != null)
  50.             {
  51.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  52.             }
  53.         }
  54.         public event PropertyChangedEventHandler PropertyChanged;
  55.     }
  56.  
  57.     class DataComponent : INotifyPropertyChanged
  58.     {
  59.         public DataComponent()
  60.         {
  61.         }
  62.         public bool BooleanVal
  63.         {
  64.             get { return boolExample; }
  65.             set {
  66.                 boolExample = value;
  67.                 this.OnPropertyChanged("BooleanVal");
  68.             }
  69.         }
  70.         public float FloatVal
  71.         {
  72.             get { return valExample; }
  73.             set
  74.             {
  75.                 valExample = value;
  76.                 this.OnPropertyChanged("FloatVal");
  77.             }
  78.         }
  79.         bool boolExample = false;
  80.         float valExample = 0.0f;
  81.  
  82.         public void OnPropertyChanged(string propertyName)
  83.         {
  84.             if (PropertyChanged != null)
  85.             {
  86.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  87.             }
  88.         }
  89.         public event PropertyChangedEventHandler PropertyChanged;
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement