其他分享
首页 > 其他分享> > 我如何获取单选按钮的值?

我如何获取单选按钮的值?

作者:互联网

因此,这是单选按钮的选项(名称):

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

如何根据用户在单选按钮中设置的值设置乘数的值?

解决方法:

如错误消息中所述,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);
    }
}

标签:c,radio-button
来源: https://codeday.me/bug/20191122/2059109.html