Groups | Blog | Home
all groups > dotnet windows forms databinding > november 2006 >

dotnet windows forms databinding : Binding RadioButtons: Suggestions?



Jamie Risk
11/24/2006 3:35:02 PM
Through the help of this newsgroup I've added capability
to have my forms update if the bound data source is updated
from a different thread. Fantastic!

However, not all my controls are bound to the data, particularly
the RadioButtons. As an example, I have an enumerated value that
is set from a selection of RadioButtons on their own panel:

//...
enum tempUnits
{ None, Celcius, Fahrenheit, Kelvin, Rankin, Centigrade }

//...
public tempUnits unitSelection;

private void None_radioButton_CheckedChanged(...)
{ unitSelection = tempUnits.None; }

private void Celcius_radioButton_CheckedChanged(...)
{ unitSelection = tempUnits.Celcius; }
//etc.


How can I bind the RadioButtons as a control to the data?


Bart Mermuys
11/24/2006 11:31:23 PM
Hi,

[quoted text, click to view]

You could create your own panel, which exposes the value (RadioValue) of the
checked radio button.

[ProvideProperty("Value", typeof(RadioButton))]
public class RadioPanel : Panel, IExtenderProvider
{
private object radioValue;
private Dictionary<RadioButton, object> radioValues =
new Dictionary<RadioButton, object>();

public object RadioValue
{
get
{
return radioValue;
}
set
{
if (!object.Equals(radioValue, value))
{
radioValue = value;

if (RadioValueChanged != null)
RadioValueChanged(this, EventArgs.Empty);

foreach (KeyValuePair<RadioButton, object> entry in radioValues)
{
if (object.Equals(entry.Value, radioValue))
entry.Key.Checked = true;
}
}
}
}

public event EventHandler RadioValueChanged;

[TypeConverter(typeof(StringConverter))]
public void SetValue(RadioButton Button, object Value)
{
radioValues[Button] = Value;
}

[TypeConverter(typeof(StringConverter))]
public object GetValue(RadioButton Button)
{
return radioValues.ContainsKey(Button) ? radioValues[Button] : null;
}

#region IExtenderProvider Members

public bool CanExtend(object extendee)
{
return typeof(RadioButton).IsAssignableFrom(extendee.GetType()) &&
Controls.Contains((RadioButton)extendee);
}

#endregion

protected override void OnControlAdded(ControlEventArgs e)
{
RadioButton rb = e.Control as RadioButton;
if (rb != null)
rb.CheckedChanged += new EventHandler(OnRadioChecked);

base.OnControlAdded(e);
}

protected override void OnControlRemoved(ControlEventArgs e)
{
RadioButton rb = e.Control as RadioButton;
if (rb != null)
rb.CheckedChanged -= new EventHandler(OnRadioChecked);

base.OnControlRemoved(e);
}

private void OnRadioChecked(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;

if (rb.Checked)
RadioValue = GetValue(rb);
}
}


How to use:
- drag the RadioPanel from the Toolbox on the Form

- drag some RadioButtons on the RadioPanel, using the designer you can only
set a string value for each RadioButton, but from code you can set any
value, like this:
radioPanel1.SetValue( None_radioButton, tempUnits.None );
radioPanel1.SetValue( Celcius_radioButton, tempUnits.Celcius );
....

- then you can bind the RadioPanel's RadioValue like any other control:
radioPanel1.DataBindings.Add("RadioValue", someObject or someList,
"someProperty");

You can't bind to fields only to properties, but the Form can be an object
DataSource, if that's what you want. And if you bind to a DataTable, then
the fields are integers not enums, so you would need to cast each enum to an
integer when setting the RadioButton values.


HTH,
Greetings


[quoted text, click to view]

Jamie Risk
11/27/2006 12:48:40 PM
Fantastic!

Thanks, I was able to implement without any trouble at all,
removed lots of silly code in and learned a number of things in
the process...

- Jamie

[quoted text, click to view]

....

[quoted text, click to view]
AddThis Social Bookmark Button