Wildhorn
Wildhorn

Reputation: 932

RadioButton and switch

I have many radio buttons so I end up with code that look like this

if (rbFrenchImp.Checked)
{
}
else if (rbFrenchMet.Checked)
{
}
else if (rbEnglishImp.Checked)
{
}
else if (rbFrenchEuro.Checked)
{
}
//etc...

So I was wondering, is it possible to use a switch case with radio button? If yes how?

Upvotes: 5

Views: 32479

Answers (8)

Leander
Leander

Reputation: 1

In each RadionButton properties set actionCommand a value (1, 2, 3...) then get the name of the Button Group:

switch(Integer.parseInt(buttonGroup.getSelection().getActionCommand()))
    {
    case 1:
         do something...
         break;
    case 2:
         do somenthing...
         break;
    case 3:
         do somenthing...
         break;
    default:
  
    }

Upvotes: 0

buddybubble
buddybubble

Reputation: 1319

Here is a Solution using Linq:

var rbResult = from rb in gbState.Controls.OfType<RadioButton>() 
where rb.Checked 
select rb;

This will give you a <IENumerable>RadioButton. As your Radiobuttons should be in a Container like a GroupBox, this IENumberable can contain only 1 or 0 elements.

I think this should work. If it does not, please correct me

Upvotes: 0

Dan Ergis
Dan Ergis

Reputation: 385

Here is the best method I found for changing an "if statement" with multiple radio buttons into a case switch. All you need to do with the radio button is select the event tab under properties and select radioButton_Click under the "Checked" event. I hope this will be helpful to someone in the future.

private void radioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioBtn = (RadioButton)sender;
            if (radioBtn.IsChecked == true)
            {
                switch (radioBtn.Name)
                {
                    case "radioBtnName1":
                        //do something
                        break;

                    case "radioBtnName2":
                        //do something
                        break;

                    case "radioBtnName3":
                        //do something
                        break;

                    case "radioBtnName4":
                        //do something
                        break;
                }

            }


        }

Upvotes: 2

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158319

Feels slightly hacky, but you could set up a Dictionary<RadioButton, Action> containing a mapping between RadioButton controls and corresponding methods (that are parameterless void methods, matching the Action delegate signature):

Dictionary<RadioButton, Action> _rbMethods = new Dictionary<RadioButton, Action>();

When loading the form, add the buttons and corresponding methods to the map:

_rbMethods.Add(rbFrenchImp, FrenchImpAction);
_rbMethods.Add(rbFrenchMet, FrenchMetAction);
_rbMethods.Add(rbEnglishImp, EnglishImpAction);

Then, you can quite easily invoke corresponding methods for the checked radio button (or buttons, in case they are in different groups):

foreach (RadioButton key in _rbMethods.Keys.Where(rb => rb.Checked))
{
    _rbMethods[key]();
}

Upvotes: 4

Pabuc
Pabuc

Reputation: 5638

//create a global radioCheckStatus
RadioCheckStatus radioStatus = RadioCheckStatus.none;

//declare your enum
private enum RadioCheckStatus
{
      none = 0,
      rbFrenchImp = 1,
      rbFrenchMet = 2,
      rbEnglishImp = 3,
      rbFrenchEuro = 4
}

//On CheckedChanged event of your radioButtons set it to changed radioButtons enum
private void rbFrenchImp_CheckedChanged(object sender, EventArgs e)
{
      radioStatus = RadioCheckStatus.rbFrenchImp;
}

// whereever you want to check the selected value
switch (radioStatus)
{
      case RadioCheckStatus.rbFrenchImp:
      {
             break;
      }
      case RadioCheckStatus.rbFrenchEuro:
      {
             break;
      }
      case RadioCheckStatus.rbEnglishImp:
      {
             break;
      }
      case RadioCheckStatus.rbFrenchMet:
      {
             break;
      }
      default:
             break;
}

Upvotes: 0

rpfaraco
rpfaraco

Reputation: 249

IF you are using ASP.NET you can use radiobuttonlist and switch like in this example dont´t know about windows form or wpf

Upvotes: 0

Liviu Mandras
Liviu Mandras

Reputation: 6627

Yes you can:

You subcribe the same CheckChanged(or similar) event handler to each of the radio buttons. Then you put some code like this:

RadioButton btn = sender as RadioButton;
if(btn!= null && btn.IsChecked)
{
   Switch(btn.Name)
   {
      case "rbFrenchImpl":
      break;
      ...
   }
}

This works on all type of frameworks.

Upvotes: 7

Noon Silk
Noon Silk

Reputation: 55082

Do you mean a RadioButtonList? If so, you can just look for .SelectedValue.

Otherwise, if you've just got a selection of radio buttons, you're only option will be to, yes, loop over them in some fashion. I'd guess, however, that you can probably replace the scheme you have with a RadioButtonList to achieve some sort of success.

Upvotes: 1

Related Questions