输入字符串的格式不正确(C#:单选按钮转换)
Input String was not in Correct Format (C#: Radio buttons conversion)
我正在制作一个简单的程序,其中 I/the 用户将 select 一个数字(单选按钮),当单击 "Click" 时,将出现一个消息框并显示结果。
我快到了,但每次单击 "Click" 时,都会发生错误并提示:输入的字符串格式不正确。
会有什么问题?
这是我的代码。
private void button1_Click(object sender, EventArgs e)
{
try
{
int multiply = 20;
String rad1 = Convert.ToString(radioButton1);
int rad11 = Convert.ToInt16(rad1); //it says here that this is a wrong format.
// int product;
if (radioButton1.Checked)
{
int product = rad11 * multiply;
String answer = Convert.ToString(product);
MessageBox.Show(answer);
}
}
catch (Exception eb)
{
MessageBox.Show(eb.Message);
}
}
如果您使用的是 Winforms 或 ASP 那么您需要做
String rad1 = radioButton1.Text;
如果你使用的是 WPF,那么你需要做
String rad1 = Convert.ToString(radioButton1.Content);
在 Convert.ToString(radioButton1) 中调试时你得到了什么值?
根据我的说法,你不能转换单选按钮,而是可以转换文本或者你可以说值
我正在制作一个简单的程序,其中 I/the 用户将 select 一个数字(单选按钮),当单击 "Click" 时,将出现一个消息框并显示结果。
我快到了,但每次单击 "Click" 时,都会发生错误并提示:输入的字符串格式不正确。
会有什么问题?
这是我的代码。
private void button1_Click(object sender, EventArgs e)
{
try
{
int multiply = 20;
String rad1 = Convert.ToString(radioButton1);
int rad11 = Convert.ToInt16(rad1); //it says here that this is a wrong format.
// int product;
if (radioButton1.Checked)
{
int product = rad11 * multiply;
String answer = Convert.ToString(product);
MessageBox.Show(answer);
}
}
catch (Exception eb)
{
MessageBox.Show(eb.Message);
}
}
如果您使用的是 Winforms 或 ASP 那么您需要做
String rad1 = radioButton1.Text;
如果你使用的是 WPF,那么你需要做
String rad1 = Convert.ToString(radioButton1.Content);
在 Convert.ToString(radioButton1) 中调试时你得到了什么值? 根据我的说法,你不能转换单选按钮,而是可以转换文本或者你可以说值