Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem using switch/case with radio buttons in C#

1,155 views
Skip to first unread message

Jordan Parmley

unread,
Oct 28, 2009, 1:01:22 AM10/28/09
to
Hi,

I'm having a problem doing a switch/case statement for my radio button(s). The gist of the program is to have the user select any two radio buttons from 2 group boxes with 10 radio buttons in each group box. The user picks 1-10 on how good the quality of the product was, then the program will add all the tallies up and give the results on a separate form.

I can use if/else to finish this but I'd rather figure it out using a switch statement - no matter how difficult this is.

How would I go about doing this? Since the only radio buttons on the form are in group boxes is there a way I can go about using that to my advantage?

Thanks to anyone who takes the time to help me out with this problem.

From http://www.developmentnow.com/g/36_0_0_0_0_0/dotnet-languages-csharp.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

Peter Duniho

unread,
Oct 28, 2009, 2:38:52 AM10/28/09
to
Jordan Parmley wrote:
> Hi,
>
> I'm having a problem doing a switch/case statement for my radio button(s).

And that problem, specifically, is?

> [...]


> I can use if/else to finish this but I'd rather figure it out using a switch statement - no matter how difficult this is.

Really? No matter _how_ difficult it is, you'd rather use a switch
statement? That's brave. :)

> How would I go about doing this? Since the only radio buttons on the form are in group boxes is there a way I can go about using that to my advantage?

Probably not. I mean, radio buttons generally are found grouped in some
container, but the Designer doesn't actually enforce a specific order.
So while you could enumerate the child controls of the container, it's
not really possible to know for sure they will be enumerated in the
expected order. You can, of course, always be very careful to make sure
they have the correct order in the Designer, but that's a big
maintenance headache IMHO.

That said, there are lots of other alternatives. Three that come to
mind immediately:

-- Use the Tag property to associate some data with each radio
button (e.g. a name or index), that you can then reference in your
switch statement. The Designer will only let you store a string, but
you can actually store any object reference. If you wanted an index but
still wanted to edit the Tag in the Designer, a simple loop during
initialization to parse the string and convert it to a boxed int
assigned to the Tag property would work fine.

-- Store the radio button references in an array, then switch on
the array index of the specific radio button, after having scanned the
array to find the specific button that's been selected.

-- Create an event handler dealing with the selection changes that
takes as an argument the index of the radio button, then wrap that with
an anonymous method that is the actual radio button's event handler,
passing a different value for each radio button you're using. For bonus
points, combine this method with the previous one, using an array of
radio buttons but only for the initialization of the event handler
(watch out for variable capturing!). :)

Pete

kndg

unread,
Oct 28, 2009, 2:41:40 AM10/28/09
to

Hi Jordan,

.Net 3.5 only,

var n1 = groupBox1.Controls.OfType<RadioButton>().First(p => p.Checked).Tag;
var n2 = groupBox2.Controls.OfType<RadioButton>().First(p => p.Checked).Tag;
var total = Int32.Parse(n1.ToString()) + Int32.Parse(n2.ToString());
MessageBox.Show(total.ToString());

Regards.

kndg

unread,
Oct 28, 2009, 2:44:36 AM10/28/09
to
kndg wrote:
> Jordan Parmley wrote:
>> Hi,
>>
>> I'm having a problem doing a switch/case statement for my radio
>> button(s). The gist of the program is to have the user select any two
>> radio buttons from 2 group boxes with 10 radio buttons in each group
>> box. The user picks 1-10 on how good the quality of the product was,
>> then the program will add all the tallies up and give the results on a
>> separate form.
>>
>> I can use if/else to finish this but I'd rather figure it out using a
>> switch statement - no matter how difficult this is.
>> How would I go about doing this? Since the only radio buttons on the
>> form are in group boxes is there a way I can go about using that to my
>> advantage?
>>
>> Thanks to anyone who takes the time to help me out with this problem.
>>
>> From
>> http://www.developmentnow.com/g/36_0_0_0_0_0/dotnet-languages-csharp.htm
>>
>> Posted via DevelopmentNow.com Groups
>> http://www.developmentnow.com/g/
>
> Hi Jordan,
>
> ..Net 3.5 only,

>
> var n1 = groupBox1.Controls.OfType<RadioButton>().First(p =>
> p.Checked).Tag;
> var n2 = groupBox2.Controls.OfType<RadioButton>().First(p =>
> p.Checked).Tag;
> var total = Int32.Parse(n1.ToString()) + Int32.Parse(n2.ToString());
> MessageBox.Show(total.ToString());
>
> Regards.

Forgot to say that you have to set the Tag property of your radioButton
control to your defined value (1-10).

Basarat

unread,
Oct 28, 2009, 2:56:28 AM10/28/09
to

Assuming that you are using winforms you can use the following. Handle
the CheckChanged event for all the radioButtons to the following
function:

private void radioButton_CheckedChanged(object sender,
EventArgs e)
{
RadioButton senderControl = sender as RadioButton;
if(!senderControl.Checked)
return;

switch ((sender as RadioButton).Text)
{
case "radioButton1":
//your code here
MessageBox.Show("test");
break;
case "radioButton2":
MessageBox.Show("test2");
break;
}
}

0 new messages