user15023500
user15023500

Reputation: 13

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?

Upvotes: 1

Views: 191

Answers (1)

nvoigt
nvoigt

Reputation: 77354

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());
    }
}

Upvotes: 5

Related Questions