当textBox2.Text和textBox1.Text为空时,我按=号就出错了
未处理的“System.FormatException”类型的异常出现在 mscorlib.dll 中。其他信息: 输入字符串的格式不正确。哪位大虾知道这是怎么回事namespace 计算机器
{
public partial class 计算机器 : Form
{
public 计算机器()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text == null || textBox1.Text == null)
{
return;
}
else
{
double a, b, c;
a = double.Parse(textBox1.Text);
b = double.Parse(textBox2.Text); if (comboBox1.Text == "加")
{
c = a + b;
textBox3.Text = c.ToString();
}
if (comboBox1.Text == "减")
{
c = a - b;
textBox3.Text = c.ToString();
}
if (comboBox1.Text == "乘")
{
c = a * b;
textBox3.Text = c.ToString();
}
if (comboBox1.Text == "除")
{
c = a / b;
textBox3.Text = c.ToString();
}
} } }
}
未处理的“System.FormatException”类型的异常出现在 mscorlib.dll 中。其他信息: 输入字符串的格式不正确。哪位大虾知道这是怎么回事namespace 计算机器
{
public partial class 计算机器 : Form
{
public 计算机器()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text == null || textBox1.Text == null)
{
return;
}
else
{
double a, b, c;
a = double.Parse(textBox1.Text);
b = double.Parse(textBox2.Text); if (comboBox1.Text == "加")
{
c = a + b;
textBox3.Text = c.ToString();
}
if (comboBox1.Text == "减")
{
c = a - b;
textBox3.Text = c.ToString();
}
if (comboBox1.Text == "乘")
{
c = a * b;
textBox3.Text = c.ToString();
}
if (comboBox1.Text == "除")
{
c = a / b;
textBox3.Text = c.ToString();
}
} } }
}
再想问个问题
if (textBox2.Text == null || textBox1.Text == null)
{
return;
}这里面如果要弹出一个消息框“不能为空!”
该怎么做?
{
MessageBox.Show("不能为空!");
textBox2.Focus();
return;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString() != "\b" && !(char.IsDigit(e.KeyChar)))
{
e.Handled = true;
} }
为什么我这样写之后,然后在textBox1中还是能输入英文呢,编译通过没有地方出错,很奇怪?
MessageBox.Show("不能为空!")