473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12215
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1459
by: Jeff | last post by:
I am having a behaviour problem with Databinding on a C# WinForm. I have three textboxes and a Checkbox bound to a dataset. The desired behaviour is when I check/uncheck the checkbox, I want to...
3
4847
by: Randy | last post by:
Hello, I'm creating a table on the fly that is used by a datagrid. I'm also creating a tableStyle that is used for the datagrid to make it look like I want. I'm using the DataGridBoolColumn to...
1
1302
by: Baskar RajaSekharan | last post by:
Hi, I have one Doubt in C#. I bound one CheckBox control to the Dataset column. when Leave the Checkbox using the Tab, the Value of the Check box is automaticllay set it to Zer 0r False. ...
0
2389
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
10
11586
by: aualias | last post by:
This seems like such a lame question, but how do you bind to a CheckBox with ASP.NET? With Windows Forms there is a DataBindings collection. I do not see it with the web version of the control,...
2
2713
by: Martin | last post by:
Hi, Please can somebody explain how databinding is done on a checkbox list. I have the follwoing code which I would have thought was enough to databind a checkbox, but apparently not. The...
3
5210
by: Moe Sizlak | last post by:
Hi, Can I check a checkbox by default based on the value returned from a database? for example if the value (dr("frm7value")) returned from a db field is "YES" how can I check the checkbox by...
0
1028
by: dotnettester | last post by:
I have a DataGrid with a checkbox as ItemTemplate.... <asp:DataGrid id="Datagrid3" runat="server" AutoGenerateColumns="False" > <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:CheckBox...
1
2342
by: Tom | last post by:
Hi there I have a little problem here, wich i think is quite strange... I have a dataset, filled from my SQL database. In it are some boolean fields (or bit as SQL server calls them) Now, on...
1
1090
by: JeffN825 | last post by:
Hi, I've been struggling with a databinding nuance for a bunch of hours now, so I figured I'd post out for help. I need to databind a control to an object. The control does not have a parent. ...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.