tags:

views:

336

answers:

4

I work on C# window vs05....For my application i need to create four radio button .....My form looks like:

A(Radio Button)  
B(Radio Button)
C(Radio Button)
D(Radio Button)


Submit (Button)

When user click the submit button i need to know which radio button is checked.....hope i got the help?

A: 

RadioButton.Checked?

pb
...Probably better as a comment.
Robert Harvey
+1  A: 

I would add all the radio buttons to a List<RadioButton> which would help you innumerate through them when the submit is checked to figure out which one is checked.

Keivan
+1  A: 

You can use the Checked Property of a RadioButton to see if it is checked:

bool isChecked = radA.Checked;

I often use the following helper function to get exactly the RadioButton which is checked:

public RadioButton GetCheckedRadioButton(Control container)
{
    if (container == null) {
        return null;
    }
    else if (container is RadioButton) {
        return GetCheckedRadioButton(container.Parent);
    }
    else {
        foreach (Control childControl in container.Controls) {
            if (childControl is RadioButton) {
                RadioButton radioBtn = (RadioButton) childControl;

                if (radioBtn.Checked) {
                    return radioBtn;
                }
            }
        }

        return null;
    }
}

Then, you can simply call this function using one of your controls or it's container, and do a switch statement, as such:

switch(GetCheckedRadioButton(radA)) {
    case radA:
        // radA is checked
        break;
    case radB:
        // radB is checked
        break;
}

Personally, I find it less verbose than the usual:

if(radA.Checked) {
   //radA is checked
}
else if(radB.Checked) {
   //radB is checked
}
Andrew Moore
A: 

If you know for sure that you will only need 4 RadioButtons, then I'd just write code similar to this.

if (Radio1.Checked) ...
if (Radio2.Checked) ...
if (Radio3.Checked) ...
...

If you know that only one of them can be checked at the same time (using a group box or some other mechanism) then you can optimise this by using "else if" instead.

However if there is a possibility that you will need more, this can get wasteful, so I agree with other answers, you should put them in a collection of your choice and use it to loop through them, to see which ones are checked.

Rekreativc