2013-12-17 51 views
3

因此,这里有我的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中设置的乘数来设置乘数的值?

+0

什么是错误信息??? – 2013-12-17 00:05:06

+0

@ChristmasUnicorn编辑。 – puretppc

回答

3

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

如果用户选择另一个单选按钮,该怎么办?它会根据单选按钮的选择制作乘数吗?所以如果用户选择第4个单选按钮会乘以0.5? – puretppc

+0

@puretppc检查我的编辑 – 2013-12-17 00:17:12

+0

非常感谢它的工作:) – puretppc

0

radioButton1.Text会给你的价值的选定项目。