469,314 Members | 2,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

Databinding checkbox <-> MyClass.Bool

Hi NG,

in a dynamically created tabpage I have a checkbox, which with its "Checked"
is bound (DataBindings.Add) to a bool property of one of my classes.
I realized, that the bool property of my class only gets updated if the
checkbox looses the focus.

Is there a way to forward the "Checked" to my class in the moment the user
clicks on the cbox, with out going the detour of the click-Event?

Thanks for help and hints.

Regards
Rainer
May 22 '06 #1
4 11896
In 2.0, Implement INotifyPropertyChanged (correctly); the following seems to
work where-ever the focus (i.e. put the focus where you want and wait 2.5
seconds...):

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;

static class Program {
static void Main() {
SomeObject obj = new SomeObject();
obj.PropertyChanged += delegate(object sender,
PropertyChangedEventArgs args) {
// when anything changed, show new value via reflection
Debug.WriteLine(sender.GetType().GetProperty(args. PropertyName).GetValue(sender,null),
args.PropertyName);
};
using (Form f = new Form())
using (Timer timer = new Timer())
{
f.Text = "Binding demo";
// add a data-bound checkbox with the value
CheckBox chk = new CheckBox();
chk.Dock = DockStyle.Top;
chk.Text = "SomeBool";
f.Controls.Add(chk);
chk.DataBindings.Add("Checked", obj, "SomeBool");

// add a button to set the value
Button btn = new Button();
btn.Text = "Set";
btn.Click += delegate { obj.SomeBool = true; };
btn.Dock = DockStyle.Top;
f.Controls.Add(btn);

// add a button to clear the value
btn = new Button();
btn.Text = "Clear";
btn.Click += delegate { obj.SomeBool = false; };
btn.Dock = DockStyle.Top;
f.Controls.Add(btn);

// configure the timer to toggle the value regularly
timer.Interval = 2500;
timer.Tick += delegate { obj.SomeBool = !obj.SomeBool; };
timer.Start();

// show
f.ShowDialog();
}
}
class SomeObject : INotifyPropertyChanged {
protected void OnNotifyPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private bool _someBool;
public bool SomeBool {
get { return _someBool; }
set {
if (_someBool != value) {
_someBool = value;
OnNotifyPropertyChanged("SomeBool");
}
}
}

public event PropertyChangedEventHandler PropertyChanged;
}
}
May 22 '06 #2
Darnit - read the e-mail backwards!

I don't know why, but I read it as "update the checkbox as soon as the
object changes". You clearly meant the exact opposite.

In this case, just change the binding's DataSourceUpdateMode; with the
previous code, this would be an extension of the existing DataBindings.Add
line:

chk.DataBindings.Add("Checked", obj, "SomeBool").DataSourceUpdateMode =
DataSourceUpdateMode.OnPropertyChanged;

Marc
May 22 '06 #3
Hi Marc,

"Marc Gravell" <ma**********@gmail.com> schrieb im Newsbeitrag
news:uB**************@TK2MSFTNGP05.phx.gbl...
Darnit - read the e-mail backwards!

I don't know why, but I read it as "update the checkbox as soon as the
object changes". You clearly meant the exact opposite.

In this case, just change the binding's DataSourceUpdateMode; with the
previous code, this would be an extension of the existing DataBindings.Add
line:

chk.DataBindings.Add("Checked", obj, "SomeBool").DataSourceUpdateMode =
DataSourceUpdateMode.OnPropertyChanged;

Works perfect. Thanks for the hint!

Rainer
May 22 '06 #4
Welcome... I just wish I hadn't done all that typing for the other
(irrelevant) answer! Well, maybe somebody will find it useful some day...

Marc
May 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jeff | last post: by
3 posts views Thread by Randy | last post: by
1 post views Thread by Baskar RajaSekharan | last post: by
reply views Thread by mike | last post: by
10 posts views Thread by aualias | last post: by
reply views Thread by dotnettester | last post: by
1 post views Thread by Tom | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.