using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace winform__计算器
{
public partial class Form1 : Form
{
int i;//声明一个全局变量
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//加
i = 1;
}
private void button2_Click(object sender, EventArgs e)
{
//减
i = 2;
}
private void button3_Click(object sender, EventArgs e)
{
//乘
i = 3;
}
private void button4_Click(object sender, EventArgs e)
{
//除
i = 4;
}
private void button5_Click(object sender, EventArgs e)
{
double num1, num2, result = 0;//声明三个变量,二个是用来计算的,一个是结果
num1 = double.Parse(t1.Text);//获取第一个对话框的值并转换成double类型
num2 = double.Parse(t2.Text);//获取第二个对话框的值并转换成double类型
switch (i)//通过i来判断该执行的运算
{
case 1: result = num1 + num2; break;
case 2: result = num1 - num2; break;
case 3: result = num1 * num2; break;
case 4: result = num1 / num2; break;
default: MessageBox.Show("请选择要执行的运算"); break;
}
t3.Text = Convert.ToString(result);//把结果显示在第三个对话框里
i = 0;//把i设回为0
}
}
}