11

I was wondering if there's any proper way of reading RadioButton that's checked from one GroupBox. So far I would create something along this lines per each GroupBox.

    private int checkRadioButton() {
        if (radioButtonKwartal1.Checked) {
            return 1;
        } else if (radioButtonKwartal2.Checked) {
            return 2;
        } else if (radioButtonKwartal3.Checked) {
            return 3;
        } else if (radioButtonKwartal4.Checked) {
            return 4;
        }
        return 0;

    }

Edit: there are some preety good answers but knowing which radioButton is pressed is one thing, but knowing the return value attached to it is 2nd. How can I achieve that? The code above lets me get return values which then I can use later in a program.

MadBoy
  • 10,824
  • 24
  • 95
  • 156

3 Answers3

14

You can use LINQ

var checkedButton = container.Controls.OfType<RadioButton>().Where(r => r.IsChecked == true).FirstOrDefault();

This assumes that you have all of the radio buttons be directly in the same container (eg, Panel or Form), and that there is only one group in the container.

Otherwise, you could make List<RadioButton>s in your constructor for each group, then write list.FirstOrDefault(r => r.Checked)

Which Radio button in the group is checked?

Community
  • 1
  • 1
Priyank
  • 10,503
  • 2
  • 27
  • 25
  • The only thing I need to know now is how to know which value is assigned to which radiobutton ? :-) Because I know now that first radio button is chosen but I have no clue what value should I assign to it so I will need method to apply `1` to first radioButton, `2` to 2nd radiobutton and so on. – MadBoy Apr 19 '11 at 15:21
  • @MadBoy: I would name them such a way that I can substring their name and identify their sequence number. e.g. rdButton1, rdButton2, etc.. and substring on name will tell you 1, 2, etc.. Does this make sense? – Priyank Apr 19 '11 at 15:41
  • 1
    I was also thinking about putting value I need in a `Tag`. Not sure what for that option is there so `return Convert.ToInt32(checkedButton.Tag);` so not sure if it won't break anything built in. – MadBoy Apr 19 '11 at 15:47
  • 4
    -1 If you are going to copy someone else's answer, you should probably acknowledge their work and build on it somehow. http://stackoverflow.com/questions/1797907/which-radio-button-in-the-group-is-checked – Yetti Apr 19 '11 at 15:48
  • Sure. thanks but there is actually a syntax change. original syntax is not right. that wasn't the intention. Edited. – Priyank Apr 19 '11 at 15:56
0

An alternative is to wire up all the RadioButtons to a single event and manage the state when they are clicked. Following code is extracted from MSDN:

void radioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;

    if (rb == null)
    {
        MessageBox.Show("Sender is not a RadioButton");
        return;
    }

    // Ensure that the RadioButton.Checked property
    // changed to true.
    if (rb.Checked)
    {
        // Keep track of the selected RadioButton by saving a reference
        // to it.
        selectedrb = rb;
    }
}

http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.aspx

Martin
  • 39,569
  • 20
  • 99
  • 130
-1

You could use the CheckedChanged event to create your own tracker.

From MSDN:

void radioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;

    if (rb == null)
    {
        MessageBox.Show("Sender is not a RadioButton");
        return;
    }

    // Ensure that the RadioButton.Checked property
    // changed to true.
    if (rb.Checked)
    {
        // Keep track of the selected RadioButton by saving a reference
        // to it.
        selectedrb = rb;
    }
}

You would need to create a Dictionary of GroupBoxes or something to store the selected radio-button per group, where the group is assumed to be rb.Parent.

RB.
  • 36,301
  • 12
  • 91
  • 131