### C# WinForms 实现加减乘除功能的计算器代码示例
以下是基于 C# WinForms 的完整实现,包括 `Form1.cs` 和 `Form1.Designer.cs` 文件的内容。这些文件定义了一个简单的桌面应用程序,能够执行基本的四则运算(加、减、乘、除)。
---
#### Form1.cs
这是主窗体的核心逻辑部分,负责处理用户输入和计算结果。
```csharp
using System;
using System.Windows.Forms;
namespace CalculatorApp
{
public partial class Form1 : Form
{
private double resultValue = 0; // 存储中间结果
private string operationPerformed = ""; // 当前操作符
private bool isOperationPressed = false; // 标记是否已按下操作符
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 处理数字按钮点击事件
/// </summary>
private void Button_Click(object sender, EventArgs e)
{
if ((textBox_Result.Text == "0") || (isOperationPressed))
textBox_Result.Clear();
isOperationPressed = false;
Button button = (Button)sender;
textBox_Result.Text += button.Text;
}
/// <summary>
/// 设置操作符并存储当前值
/// </summary>
private void Operator_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
operationPerformed = button.Text;
resultValue = Double.Parse(textBox_Result.Text);
label_CurrentOperation.Text = resultValue + " " + operationPerformed;
isOperationPressed = true;
}
/// <summary>
/// 执行等于操作
/// </summary>
private void Button_Equal_Click(object sender, EventArgs e)
{
try
{
switch (operationPerformed)
{
case "+":
textBox_Result.Text = (resultValue + Double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
textBox_Result.Text = (resultValue - Double.Parse(textBox_Result.Text)).ToString();
break;
case "*":
textBox_Result.Text = (resultValue * Double.Parse(textBox_Result.Text)).ToString();
break;
case "/":
if (Double.Parse(textBox_Result.Text) != 0)
textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
else
throw new DivideByZeroException("Cannot divide by zero.");
break;
default:
break;
}
label_CurrentOperation.Text = "";
}
catch (DivideByZeroException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Clear_All(sender, e);
}
}
/// <summary>
/// 清空所有内容
/// </summary>
private void Clear_All(object sender, EventArgs e)
{
textBox_Result.Text = "0";
resultValue = 0;
operationPerformed = "";
label_CurrentOperation.Text = "";
}
/// <summary>
/// 删除最后一个字符
/// </summary>
private void Button_BackSpace_Click(object sender, EventArgs e)
{
if (textBox_Result.Text.Length > 0)
{
textBox_Result.Text = textBox_Result.Text.Remove(textBox_Result.Text.Length - 1, 1);
if (string.IsNullOrEmpty(textBox_Result.Text))
textBox_Result.Text = "0";
}
}
}
}
```
---
#### Form1.Designer.cs
这部分是自动生成的设计器代码,定义了窗口布局和控件属性。
```csharp
namespace CalculatorApp
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.textBox_Result = new System.Windows.Forms.TextBox();
this.label_CurrentOperation = new System.Windows.Forms.Label();
this.button_Add = new System.Windows.Forms.Button();
this.button_Subtract = new System.Windows.Forms.Button();
this.button_Multiply = new System.Windows.Forms.Button();
this.button_Divide = new System.Windows.Forms.Button();
this.button_Equal = new System.Windows.Forms.Button();
this.button_ClearAll = new System.Windows.Forms.Button();
this.button_7 = new System.Windows.Forms.Button();
this.button_8 = new System.Windows.Forms.Button();
this.button_9 = new System.Windows.Forms.Button();
this.button_4 = new System.Windows.Forms.Button();
this.button_5 = new System.Windows.Forms.Button();
this.button_6 = new System.Windows.Forms.Button();
this.button_1 = new System.Windows.Forms.Button();
this.button_2 = new System.Windows.Forms.Button();
this.button_3 = new System.Windows.Forms.Button();
this.button_0 = new System.Windows.Forms.Button();
this.button_Point = new System.Windows.Forms.Button();
this.button_BackSpace = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Layout and properties of controls go here...
// Example: TextBox for displaying results
this.textBox_Result.Location = new System.Drawing.Point(12, 12);
this.Controls.Add(this.textBox_Result);
this.Controls.Add(this.label_CurrentOperation);
this.Controls.Add(this.button_Add);
this.Controls.Add(this.button_Subtract);
this.Controls.Add(this.button_Multiply);
this.Controls.Add(this.button_Divide);
this.Controls.Add(this.button_Equal);
this.Controls.Add(this.button_ClearAll);
this.Controls.Add(this.button_7);
this.Controls.Add(this.button_8);
this.Controls.Add(this.button_9);
this.Controls.Add(this.button_4);
this.Controls.Add(this.button_5);
this.Controls.Add(this.button_6);
this.Controls.Add(this.button_1);
this.Controls.Add(this.button_2);
this.Controls.Add(this.button_3);
this.Controls.Add(this.button_0);
this.Controls.Add(this.button_Point);
this.Controls.Add(this.button_BackSpace);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "Form1";
this.Text = "Simple Calculator";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox textBox_Result;
private System.Windows.Forms.Label label_CurrentOperation;
private System.Windows.Forms.Button button_Add;
private System.Windows.Forms.Button button_Subtract;
private System.Windows.Forms.Button button_Multiply;
private System.Windows.Forms.Button button_Divide;
private System.Windows.Forms.Button button_Equal;
private System.Windows.Forms.Button button_ClearAll;
private System.Windows.Forms.Button button_7;
private System.Windows.Forms.Button button_8;
private System.Windows.Forms.Button button_9;
private System.Windows.Forms.Button button_4;
private System.Windows.Forms.Button button_5;
private System.Windows.Forms.Button button_6;
private System.Windows.Forms.Button button_1;
private System.Windows.Forms.Button button_2;
private System.Windows.Forms.Button button_3;
private System.Windows.Forms.Button button_0;
private System.Windows.Forms.Button button_Point;
private System.Windows.Forms.Button button_BackSpace;
}
}
```
---
### 功能描述
- **数字输入**:通过点击数字按钮向文本框中追加数字。
- **操作符设置**:点击加、减、乘、除按钮时记录当前数值和所选操作符。
- **计算结果**:点击“=”按钮后根据选定的操作符完成相应计算并将结果显示出来。
- **清屏功能**:提供清除全部内容的功能以重新开始新的计算[^2]。
- **退格键支持**:允许删除最近一次输入的一个字符。
---