如何获取单选按钮的值

本文关键字:单选按钮 获取 何获取 | 更新日期: 2024-10-26 04:03:48

所以这是我对radio buttonoptions(名称):

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?)

如何根据用户在radio button中设置的值设置乘数的值?

如何获取单选按钮的值

如错误消息中所述,RadioButton没有 SelectedItem 属性。您应该改为获取单选按钮文本。

multiplier = Convert.ToDouble(radioButton1.Text);

如果要检查是否选择了单选按钮,请改用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);
    }
}

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