DataBinding CheckBox
I have a custom control and would like to set up databinding within the control. It has to CheckBoxes and a TextBox (not relavent at this time). I would like to bind the CheckBoxes's Checked property with the properties in my control.
Code in my class:
private System.Windows.Forms.CheckBox configurableCheckBox;
private System.Windows.Forms.CheckBox enabledCheckBox;
private bool _bConfigurable;
private bool _bMuxEnabled;
[Bindable(true, BindingDirection.TwoWay)]
[Browsable(true)]
public bool Configurable
{
get { return _bConfigurable; }
set
{
if (_bConfigurable!= value)
{
_bConfigurable = value;
}
}
}
[Bindable(true, BindingDirection.TwoWay)]
[Browsable(true)]
public bool MuxEnabled
{
get { return _bMuxEnabled; }
set
{
if (_bMuxEnabled != value)
{
_bMuxEnabled = value;
}
}
}
here is my attempt to bind the two:
public MyControl
{
this.DataBindings.Add("MuxEnabled", enabledCheckBox, "Checked");
this.DataBindings.Add("Configurable", configurableCheckBox, "Checked");
}
This doesn't work. When I click the check box, it doesn't change either property.
[1543 byte] By [
Melon00] at [2007-11-20 0:03:29]

# 1 Re: DataBinding CheckBox
The DataBindings work in the following manner . Suppose you want to bind a label (named label1) with a checkbox (checkbox1). You have to write the following simple code :
label1.DataBindings.Add("Text", checkbox1, "Checked");
Text is the text value of the label1 and Checked returns true or false ( if the checkedbox1 is checked or unchecked) .
So , DataBindings binds the property of a control with a property of another object ( like a buttons text with a label text , or a label text with a DataSet) .
From MSDN:
Use the DataBindings property to access the ControlBindingsCollection. By adding Binding objects to the collection, you can bind any property of a control to the property of an object.
When you put your custom control in another form you have to bind his properties with another object property:
myControl.DataBindings.Add("BoolProperty", checkbox1, "Checked");
When your property changes the checkbox changes its state to checked or unchecked (true or false ).
For example you have one form with those property that you mentioned
Configurable and MuxEnabled . In the form contructor place the following code:
public Form1()
{
InitializeComponent();
private bool _bConfigurable=true;
configurableCheckBox.DataBindings.Add("Checked", this, "Configurable");
}
Note: Do not place any code before InitializeComponent()
I binded the checkbox (configurableCheckBox )property "Checked" with the forms property "Configurable" ( the one that you declared bindable). Setting _bConfigurable to true checks your checkbox.
Good luck !
# 3 Re: DataBinding CheckBox
Try this (I am guessing)
enabledCheckBox.DataBindings.Add("Checked", this, "MuxEnabled");
configurableCheckBox.DataBindings.Add("Checked", this, "Configurable");