How do you bind to a group of radio buttons in Windows Forms?

T

teddysnips

Back in the dim mists of antiquity I used to program in VBA for
Microsoft Access (hey, don't knock it - very useful tool for the right
application).

This had a really handy control in the toolbox called an Option
Group. It could contain any number of radio buttons (which Access
called Option Buttons), each of which had a "value" property. The
Option Group as a whole had a property equal to the value of the
selected radio button. Thus you could bind the group to a single
datum.

Example. Let's say a "Person" record contains a column called
"PreferredTransport" (type = integer). This can take any of three
values:

0 = Car
1 = Train
2 = Bus

How simple, therefore, to create an OptionGroup called "Transport",
with three radio buttons as above and set it according to the value of
the "PreferredTransport" column.

Why isn't this available out of the box in C# for Windows Forms? Does
anyone know of a site where I could get an example of a workaround? I
know I can do it the hard way - either binding each radio button on my
form to a separate datum, or having unbound radio buttons and
explicitly setting and getting the values, but it's all a bit
laborious.

Many thanks

Edward
 
N

Nicholas Paldino [.NET/C# MVP]

Actually, I think it would be kind of easy. You can derive from
RadioButton and provide a Value property which has the value that you want
that RadioButton to have.

Then, have a class derived from Panel (or some other container control)
which has a Value property. It would cycle through its controls and for
every type of that derived radio button class that it has, it would check to
see if the radio button is checked. If it is, it returns that value.

Hope this helps.
 
T

teddysnips

Actually, I think it would be kind of easy. You can derive from
RadioButton and provide a Value property which has the value that you want
that RadioButton to have.

Then, have a class derived from Panel (or some other container control)
which has a Value property. It would cycle through its controls and for
every type of that derived radio button class that it has, it would check to
see if the radio button is checked. If it is, it returns that value.

Hope this helps.

It helps, in the sense that it tells me how to achieve my goal, or at
least gives me directions for landfall at least. What's truly
irritating is that IMHO this should be provided in the toolbox, since
groups of mutually exclusive option/radio buttons are pretty well
universal in UI design. I know some people think that drop-down lists
are better, but they're not, unless you have a lot of options (such as
a list of countries).

Oh well, back to the grindstone.

Cheers

Edward
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top