算术运算结果不以标签显示 [英] Arithmetic operation Result is not display in lable
本文介绍了算术运算结果不以标签显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void RadioButton2_CheckedChanged(对象发件人,EventArgs e)
{
int no1,no2;
float ans;
no1 = Convert.ToInt32( textbox2.Text);
no2 = Convert.ToInt32( textbox3.Text);
ans = no1 - no2;
Label2.Text = ans.ToString();
}
受保护 void RadioButton1_CheckedChanged( object sender,EventArgs e)
{
int no1,no2;
float ans;
no1 = Convert.ToInt32( textbox2.Text);
no2 = Convert.ToInt32( textbox3.Text);
ans = no1 + no2;
Label2.Text = ans.ToString();
}
受保护 void RadioButton3_CheckedChanged( object sender,EventArgs e)
{
int no1,no2;
float ans;
no1 = Convert.ToInt32( textbox2.Text);
no2 = Convert.ToInt32( textbox3.Text);
ans = no1 * no2;
Label2.Text = ans.ToString();
}
受保护 void RadioButton4_CheckedChanged( object sender,EventArgs e)
{
int no1,no2;
float ans;
no1 = Convert.ToInt16( textbox2.Text);
no2 = Convert.ToInt16( textbox3.Text);
ans = no1 / no2;
Label2.Text = ans.ToString();
}
解决方案
我不知道为什么,但你试图将文本textbox1.Text转换为一个整数,而不是文本框中的值:
protected < span class =code-keyword> void RadioButton2_CheckedChanged( object sender,EventArgs e)
{
int no1,no2;
float ans;
no1 = Convert.ToInt32(textbox2.Text); // 这就是问题所在的问题
no2 = Convert.ToInt32(textbox3.Text ); // 所有带引号的线
ans = no1 - no2;
Label2.Text = ans.ToString();
}
受保护 void RadioButton1_CheckedChanged( object sender,EventArgs e)
{
int no1,no2;
float ans;
no1 = Convert.ToInt32(textbox2.Text);
no2 = Convert.ToInt32(textbox3.Text);
ans = no1 + no2;
Label2.Text = ans.ToString();
}
受保护 void RadioButton3_CheckedChanged( object sender,EventArgs e)
{
int no1,no2;
float ans;
no1 = Convert.ToInt32(textbox2.Text);
no2 = Convert.ToInt32(textbox3.Text);
ans = no1 * no2;
Label2.Text = ans.ToString();
}
受保护 void RadioButton4_CheckedChanged( object sender,EventArgs e)
{
int no1,no2;
float ans;
no1 = Convert.ToInt16(textbox2.Text);
no2 = Convert.ToInt16(textbox3.Text);
ans = no1 / no2;
Label2.Text = ans.ToString();
}
在进行转换时取消引号;)
替换它
Convert.ToInt32( textbox2.Text );
有了这个
Convert.ToInt32(textbox2.Text);
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
int no1, no2;
float ans;
no1 = Convert.ToInt32("textbox2.Text");
no2 = Convert.ToInt32("textbox3.Text");
ans = no1 - no2;
Label2.Text = ans.ToString();
}
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
int no1, no2;
float ans;
no1 = Convert.ToInt32("textbox2.Text");
no2 = Convert.ToInt32("textbox3.Text");
ans = no1 + no2;
Label2.Text = ans.ToString();
}
protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
{
int no1, no2;
float ans;
no1 = Convert.ToInt32("textbox2.Text");
no2 = Convert.ToInt32("textbox3.Text");
ans = no1 * no2;
Label2.Text = ans.ToString();
}
protected void RadioButton4_CheckedChanged(object sender, EventArgs e)
{
int no1, no2;
float ans;
no1 = Convert.ToInt16("textbox2.Text");
no2 = Convert.ToInt16("textbox3.Text");
ans = no1 /no2;
Label2.Text =ans.ToString();
}
解决方案
I don't know why, but you are trying to convert the text "textbox1.Text" to an integer, not the value in the textbox:
protected void RadioButton2_CheckedChanged(object sender, EventArgs e) { int no1, no2; float ans; no1 = Convert.ToInt32(textbox2.Text); //THIS IS WHERE THE PROBLEM WAS ON no2 = Convert.ToInt32(textbox3.Text); //ALL THESE LINES WITH QUOTES ans = no1 - no2; Label2.Text = ans.ToString(); } protected void RadioButton1_CheckedChanged(object sender, EventArgs e) { int no1, no2; float ans; no1 = Convert.ToInt32(textbox2.Text); no2 = Convert.ToInt32(textbox3.Text); ans = no1 + no2; Label2.Text = ans.ToString(); } protected void RadioButton3_CheckedChanged(object sender, EventArgs e) { int no1, no2; float ans; no1 = Convert.ToInt32(textbox2.Text); no2 = Convert.ToInt32(textbox3.Text); ans = no1 * no2; Label2.Text = ans.ToString(); } protected void RadioButton4_CheckedChanged(object sender, EventArgs e) { int no1, no2; float ans; no1 = Convert.ToInt16(textbox2.Text); no2 = Convert.ToInt16(textbox3.Text); ans = no1 /no2; Label2.Text =ans.ToString(); }
Suppress the quotes when you're doing conversions ;)
Replace this
Convert.ToInt32("textbox2.Text");
With that
Convert.ToInt32(textbox2.Text);
这篇关于算术运算结果不以标签显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文