Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use databinding with Windows Forms radio buttons?

I have a binary field in my database that is hard to describe in a UI using a single "Is XXXX?"-type checkbox. I'd rather use a pair of radio buttons (e.g. "Do it the Foo way" and "Do it the Bar way"), but right now all the other fields on my form are data-bound to a business object. I'd like to data-bind the pair of radio buttons to the business object as well, but haven't come up with a good way to do it yet. I can bind one of the buttons to the field, such that the field is set "true" if the button is selected, but while selecting the other button does de-select the first one (that is, the two radio buttons are properly paired), the value of the field does not update to reflect this.

I'd like to be able to say

button1.DataBindings.Add(new Binding("checked", source, "useFoo"));
button2.DataBindings.Add(new Binding("checked", source, "!useFoo"));

but I'm pretty sure that will throw when it runs. Is there an easier way, or should I just put more thought into how to word a single checkbox? I don't want to add extra functions to handle something this trivial...

ETA: A commenter has suggested considering a dropdown (ComboBox). I had thought about this, but how would I data-bind that to a boolean field in a database/Property in a business object? If I bind the SelectedItem to the useFoo property, what would go in the Items collection? Would I have to add just "True" and "False", or could I somehow add a key/value pair object that ties a displayed item ("Use Foo" / "Do Not Use Foo") to the boolean value behind it? I'm having trouble finding docs on this.


About the answer: the solution I wound up using involved modifying the business object -- the basic idea is very similar to the one posted by Gurge, but I came up with it separately before I read his response. In short, I added a separate property that simply returns !useFoo. One radio button is bound to source.UseFoo, and the other is bound to source.UseBar (the name of the new property). It's important to make sure the new property has both getters and setters, or you'll wind up with really odd behavior.

like image 881
Coderer Avatar asked Dec 05 '08 20:12

Coderer


People also ask

How do you group radio buttons in Windows form application?

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.

How do I add a RadioButton to a web form?

To create RadioButton we can drag it from the toolbox of visual studio. This is a server side control and ASP.NET provides own tag to create it. The example is given below. Server renders it as the HTML control and produces the following code to the browser.

How do I group radio buttons in XAML?

You group RadioButton controls by putting them inside the same parent container or by setting the GroupName property on each RadioButton to the same value.


1 Answers

  1. Bind the RadioButton that is directly linked to your boolean value (ie is checked when the value is true).
  2. Add an event handler to the CheckedChanged event on this RadioButton that looks like the following :

    private void radioButton_CheckedChanged(object sender, EventArgs e)
    {
        foreach (Binding b in ((Control)sender).DataBindings)
            b.WriteValue();
    }
    
like image 109
Mac Avatar answered Oct 23 '22 19:10

Mac