1

i have this part of code: (in the click event of a button)

           foreach (RadioButton rb in grbFiltro.Controls.OfType<RadioButton>())
       {
           if (((RadioButton)rb).Checked)
           {
               switch (true)
               {
                   case rbt1.Checked:
                       //do something
                       break;
                   case rbt2.Checked:
                      //do something
                       break;
                   case rbt3.Checked:
                        //do something
                      break;
               }
           }
       }

but there´s a error converting radiobutton to string in ---> case rbt1.Checked

how do i fix this, or what else can i use?

greg dorian
  • 136
  • 1
  • 3
  • 14

3 Answers3

1
var rb = grbFiltro.Controls.OfType<RadioButton>()
             .SingleOrDefault(rb => rb.Checked.GetValueOrDefault()) as RadioButton;

if (rb!=null)
    switch (rb.Name)
           {
               case "button1":
                   //do something
                   break;
               case "button2":
                  //do something
                   break;
               case "button3":
                    //do something
                  break;
           }
paul
  • 21,653
  • 1
  • 53
  • 54
1

You have 25 buttons so I would probably do something like

 IEnumerable<RadioButton> buttons = grbFiltro.Controls.OfType<RadioButton>();
    foreach (var Button in buttons)
    {
         if (Button.Checked)
         {
             //Do Something
         }
     }

If you need to do something different with each button then you would have an if / else chain that does based on what you need, but I am guessing you are wanting to store the value selected and do something with that.

confusedandamused
  • 746
  • 2
  • 8
  • 28
Robert
  • 4,306
  • 11
  • 45
  • 95
  • i have more than 3 radiobuttons! 25 exactly – greg dorian Feb 07 '13 at 16:31
  • I was basing it on your above post if you have that many buttons I would probably test them in a list give me a second and I will give you some code – Robert Feb 07 '13 at 18:00
  • can I use a switch better than if()?? – greg dorian Feb 07 '13 at 22:57
  • Depending on what your trying to do I do not think you would need to chain that statement. Most of the time people just get some kind of value from a radio button and store it in a variable for later use. Basically if you wanted say the string value of a radio button called red. you would just var buttonValue = button.Text. Chaining the case out like that adds a large amount of code you don't necessarily need. – Robert Feb 08 '13 at 12:49
0

you can also add a listener on CheckedChange and always store the currently selected Radio button in a field. Then you just Need to read that value, once your confirmbutton has been clicked. This is Handy if you have more than 10 radiobuttons

private RadioButton selectedButton;

//handler for Radio Buttons.
private void onRadioChange(Object sender, EventArgs e){
    if (((RadioButton)sender).Checked)
       this.selectedButton = (RadioButton)sender;
}

// handler for confirm button.
private void confirm(Object sender, EventArgs e){
   MessageBox.show(selectedButton.name + " selected");
}

untestet, but the way should work.

dognose
  • 20,360
  • 9
  • 61
  • 107