C#窗体小程序计算器
使其能完成2个数的加、减、乘、除基本运算。界面如下图,单击相应的运算符按钮,则完成相应的运算,并将结果显示出来,同时不允许在结果栏中输入内容
代码如下:
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
textBox3.Text = (a + b).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
textBox3.Text = (a - b).ToString();
}
private void button3_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
textBox3.Text = (a * b).ToString();
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox2.Text == "0")
{
MessageBox.Show("被除数不能为0");
return;
}
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
textBox3.Text = (a / b).ToString();
}