Search code examples
c#groupbox

How can I get the true value?


enter image description here enter image description here

This is all the code:

private void button1_Click(object sender, EventArgs e)
{
    foreach (Control control in groupBox1.Controls)
    {
        MessageBox.Show(control.ToString());
    }
}

I only want to get the true value But there is no Checked. How can I get the true value? How can I get each index?


Solution

  • Right now, you have all controls, which is why they are of type control and you don't know what they are at compile-time.

    Make sure you only get those of type RadioButton:

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (RadioButton radioButton in groupBox1.Controls.OfType<RadioButton>())
        {
             MessageBox.Show(radioButton.Checked.ToString());
        }
    }