0

A large group of radio buttons exist on a form. They are not inside a panel, but for this example they are the only set of radios on the form.

At initialization, no button will be selected. (Likely, this is not proper implementation of radios, but it's outside of my control; Suffice to say, for this example, the form must start out with no radio button selected.)

Currently I use a rather lengthy if-else statement to check each button one at a time, but I feel there must be a simpler way to determine, right off the bat, if no button is checked. Besides the buttons already work as a group, because by definition a maximum of 1 selection is forced throughout.

What would be the best practice for detecting if no radio button has been selected?

4444
  • 3,541
  • 10
  • 32
  • 43
  • If they really are **radio buttons**, one of them should *always* be selected, and there should be a property in the Option Group that tells you *which* one is selected. – Robert Harvey Jul 10 '12 at 19:50
  • 1
    @RobertHarvey not on initialization. On initialization, they are all "off" – Cole Tobin Jul 10 '12 at 19:51
  • http://stackoverflow.com/questions/1788490/c-how-can-i-iterate-through-all-checkboxes-on-a-form – Michal Franc Jul 10 '12 at 19:51
  • Then set a default one on during initialization. Radio buttons are not intended to be all unchecked. – Robert Harvey Jul 10 '12 at 19:52
  • 1
    What do you mean by *the most efficient way?* – L.B Jul 10 '12 at 19:52
  • The first answer in the linked question answers your question. Check for a `null` return value. – Robert Harvey Jul 10 '12 at 19:58
  • @Robert Harvey In this specific case, I'm intending to start blank, for example, on initialization of the form, (They will remain blank until user input because selecting one of them causes a large load of data) - Can this Option Group property determine that no button has been selected? – 4444 Jul 10 '12 at 20:00
  • Yes, look at the linq statement in that answer. It should return `null` if none of the radio buttons are selected. – Robert Harvey Jul 10 '12 at 20:01
  • I cannot confirm yet that the above example worked for my code, although it may have been an error on my implementation. I did have successful use of itsme86's suggestion, though, so I'm not pursuing further. – 4444 Jul 10 '12 at 20:12
  • @L.B For future refference, L.B, "most efficient" in general reffers to the option with the most preferable ratio of manual work vs. resource-usage, while still producing a virtually-identical result to common alternatives. (My bad, I shouldn't have assumed the definition was common-knowledge.) – 4444 Jul 10 '12 at 21:02
  • @Doc, Yes your bad. My definition includes speed, memory usage, parallelism etc. – L.B Jul 10 '12 at 21:12
  • @L.B In this case, which of the given answers would your definition prove to be the most efficient? I'm always looking for ways to practice more pragmatism. – 4444 Jul 10 '12 at 21:20

2 Answers2

5

You could always create a collection of the radio buttons:

RadioButton[] radioButtons = new RadioButton[] { radioButton1, radioButton2 };

if (!radioButtons.Any(rb => rb.Checked))
    // No radio buttons are checked

Also, using this similar question (https://stackoverflow.com/questions/1797907) as a reference, you could do something like:

if(!Controls.OfType<RadioButton>().Any(rb => rb.Checked))
    // No radio buttons are checked
Community
  • 1
  • 1
itsme86
  • 19,266
  • 4
  • 41
  • 57
1

I would would guess something like this:

form.Controls.Where(x => x is RadioButton).Where(x => x.Selected == true).Count()

Or if you have them available in an array or list of some sort you don't have to loop all the controls.

I suppose you could speed it up a bit by:

form.Controls.Where(x => x is RadioButton && ((RadioButton)x).Selected == true).Count()
einord
  • 2,278
  • 2
  • 20
  • 26