这是我的options(名称),用于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?)


如何根据用户在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);
    }
}

10-05 23:35