c#窗体计算器
时间: 2023-07-10 07:14:02 浏览: 69
以下是一个简单的 C# 窗体计算器的代码示例:
```csharp
using System;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double num1, num2, result;
num1 = double.Parse(textBox1.Text);
num2 = double.Parse(textBox2.Text);
result = num1 + num2;
textBox3.Text = result.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
double num1, num2, result;
num1 = double.Parse(textBox1.Text);
num2 = double.Parse(textBox2.Text);
result = num1 - num2;
textBox3.Text = result.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
double num1, num2, result;
num1 = double.Parse(textBox1.Text);
num2 = double.Parse(textBox2.Text);
result = num1 * num2;
textBox3.Text = result.ToString();
}
private void button4_Click(object sender, EventArgs e)
{
double num1, num2, result;
num1 = double.Parse(textBox1.Text);
num2 = double.Parse(textBox2.Text);
result = num1 / num2;
textBox3.Text = result.ToString();
}
}
}
```
其中,`textBox1` 和 `textBox2` 分别用于输入两个操作数,`textBox3` 用于显示计算结果。四个按钮分别对应加、减、乘、除四种运算,单击按钮后,程序将会从 `textBox1` 和 `textBox2` 中读取输入的操作数,并进行相应的计算,最后将结果显示在 `textBox3` 中。
注意,以上代码未考虑输入错误的情况,实际应用中可能需要添加相应的错误处理机制。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)