Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Form1 : Form
- {
- DataComponent dc;
- public Form1()
- {
- InitializeComponent();
- dc = new DataComponent();
- checkBox1.CheckedChanged += (s, e) => { customControl1.Text = "3"; label1.Text = dc.BooleanVal.ToString(); };
- button1.Click += (s, e) => { dc.BooleanVal = !dc.BooleanVal; label1.Text = dc.BooleanVal.ToString(); };
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- checkBox1.DataBindings.Add("Checked", dc, "BooleanVal", false, DataSourceUpdateMode.OnPropertyChanged);
- customControl1.DataBindings.Add("BaseValue", dc, "FloatVal", false, DataSourceUpdateMode.OnPropertyChanged);
- }
- }
- class CustomControl : TextBox, INotifyPropertyChanged
- {
- public override string Text
- {
- get
- {
- return base.Text;
- }
- set
- {
- base.Text = value;
- float s;
- if (!float.TryParse(value, out s))
- s = 0.0f;
- this.BaseValue = s;
- }
- }
- public float BaseValue
- {
- get { return baseValue; }
- set
- {
- baseValue = value;
- this.OnPropertyChanged("BaseValue");
- }
- }
- float baseValue;
- public void OnPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- class DataComponent : INotifyPropertyChanged
- {
- public DataComponent()
- {
- }
- public bool BooleanVal
- {
- get { return boolExample; }
- set {
- boolExample = value;
- this.OnPropertyChanged("BooleanVal");
- }
- }
- public float FloatVal
- {
- get { return valExample; }
- set
- {
- valExample = value;
- this.OnPropertyChanged("FloatVal");
- }
- }
- bool boolExample = false;
- float valExample = 0.0f;
- public void OnPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement