winform计算器
时间: 2023-07-30 14:10:36 浏览: 124
下面是一个简单的Winform计算器的代码示例:
```csharp
using System;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
double num1, num2;
string op;
public Form1()
{
InitializeComponent();
}
private void btnNum_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
txtDisplay.Text += btn.Text;
}
private void btnOp_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
num1 = double.Parse(txtDisplay.Text);
op = btn.Text;
txtDisplay.Text = "";
}
private void btnEquals_Click(object sender, EventArgs e)
{
num2 = double.Parse(txtDisplay.Text);
double result = 0;
switch (op)
{
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
txtDisplay.Text = result.ToString();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Text = "";
num1 = 0;
num2 = 0;
}
}
}
```
这是一个基础的四则运算计算器,包含数字按钮、运算符按钮、等号按钮和清空按钮。当用户点击数字按钮时,将数字添加到文本框中。当用户点击运算符按钮时,将第一个数字保存下来,并保存运算符。当用户点击等号按钮时,将第二个数字保存下来,并根据运算符计算结果。当用户点击清空按钮时,将文本框和数字清零。
阅读全文