如何获得单选按钮的值?

时间:2013-12-17 00:04:05

标签: c# radio-button

以下是options的{​​{1}}(姓名):

radio button

我尝试使用此功能,但它给了我一个错误:

4
2
1
0.5
0.25

错误讯息:

multiplier = Convert.ToDouble(radioButton1.SelectedItem.ToString());

如何根据用户在'System.Windows.Forms.RadioButton' does not contain a definition for 'SelectedItem' and no extension method 'SelectedItem' accepting a first argument of type 'System.Windows.Forms.RadioButton' could be found (are you missing a using directive or an assembly reference?) 中设置的内容设置乘数的值?

2 个答案:

答案 0 :(得分:3)

如错误消息中所述,RadioButton没有SelectedItem属性。您应该获得radiobutton文本。

multiplier = Convert.ToDouble(radioButton1.Text);

如果要检查是否选择了radiobutton,请使用Checked属性

if (radioButton1.Checked)
{
    multiplier = Convert.ToDouble(radioButton1.Text);
}

在您的情况下,您可以使用循环

foreach (RadioButton d in this.Controls.OfType<RadioButton>())
{
    if (d.Checked)
    {
         multiplier = Convert.ToDouble(d.Text);
    }
}

答案 1 :(得分:0)

radioButton1.Text会为您提供所选项目的值。