C#实现简易计算器功能(2)(窗体应用)
前言
在上一篇教程C#实现简易计算器功能(1)中,我们使用控制台应用程序的方式实现了简易的计算器功能。但是,控制台程序的界面比较简陋,不够直观、美观。在这篇教程中,我们将使用Windows窗体应用程序的方式来实现简易计算器功能,界面将更加直观、友好。
步骤
1. 新建Windows窗体应用程序工程
打开Visual Studio,在主界面选择“创建新项目” -> “Windows桌面” -> “Windows窗体应用程序”,填写项目名称和路径,点击确定即可。
2. 构建用户界面
打开 Form1.cs 文件,我们可以看到自动生成的窗体界面。首先,我们需要设计计算器的用户界面。
本示例中的计算器界面由以下几个控件组成:
- 一个用于显示输入的文本框(textBox1)
- 数字键 0 到 9 的按钮(button0 到 button9)
- 加、减、乘、除运算符的按钮(buttonPlus,buttonMinus,buttonMul,buttonDiv)
- 小数点和清除键的按钮(buttonPoint和buttonClear)
- 等于号的按钮(buttonEqual)
在工具箱中,可以通过拖拽的方式将每个按钮控件添加到窗体中,然后设定对应的控件属性。
示例代码:
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button0;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button buttonPlus;
private System.Windows.Forms.Button buttonMinus;
private System.Windows.Forms.Button buttonMul;
private System.Windows.Forms.Button buttonDiv;
private System.Windows.Forms.Button buttonEqual;
private System.Windows.Forms.Button buttonClear;
private System.Windows.Forms.Button buttonPoint;
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button0 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.buttonPlus = new System.Windows.Forms.Button();
this.buttonMinus = new System.Windows.Forms.Button();
this.buttonMul = new System.Windows.Forms.Button();
this.buttonDiv = new System.Windows.Forms.Button();
this.buttonEqual = new System.Windows.Forms.Button();
this.buttonClear = new System.Windows.Forms.Button();
this.buttonPoint = new System.Windows.Forms.Button();
// textBox1
this.textBox1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(291, 29);
this.textBox1.TabIndex = 0;
// button0
this.button0.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button0.Location = new System.Drawing.Point(12, 175);
this.button0.Name = "button0";
this.button0.Size = new System.Drawing.Size(64, 64);
this.button0.TabIndex = 1;
this.button0.Text = "0";
this.button0.UseVisualStyleBackColor = true;
// button1
this.button1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(12, 105);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 64);
this.button1.TabIndex = 2;
this.button1.Text = "1";
this.button1.UseVisualStyleBackColor = true;
// 其他按钮略
// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(315, 372);
this.Controls.Add(this.buttonPoint);
this.Controls.Add(this.buttonClear);
this.Controls.Add(this.buttonEqual);
this.Controls.Add(this.buttonDiv);
this.Controls.Add(this.buttonMul);
this.Controls.Add(this.buttonMinus);
this.Controls.Add(this.buttonPlus);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.button0);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "简易计算器";
this.ResumeLayout(false);
this.PerformLayout();
}
3. 添加计算器逻辑代码
添加按钮的单击事件,编写逻辑代码实现计算器的功能。由于计算器的实现逻辑较为复杂,这里只给出示例代码,详细解释请参考上一篇教程。
示例代码:
public partial class Form1 : Form
{
private string operation = "";
private double num1 = 0.0, num2 = 0.0;
private bool flag = false;
public Form1()
{
InitializeComponent();
}
private void SetValue(string s)
{
if (flag == true)
{
textBox1.Text = "0";
flag = false;
}
if (textBox1.Text == "0") textBox1.Text = "";
textBox1.Text += s;
}
private void button0_Click(object sender, EventArgs e)
{
SetValue("0");
}
private void button1_Click(object sender, EventArgs e)
{
SetValue("1");
}
// 其他数字按钮略
private void buttonPlus_Click(object sender, EventArgs e)
{
operation = "+";
num1 = Convert.ToDouble(textBox1.Text.Trim());
textBox1.Text = "0";
}
private void buttonMinus_Click(object sender, EventArgs e)
{
operation = "-";
num1 = Convert.ToDouble(textBox1.Text.Trim());
textBox1.Text = "0";
}
// 其他运算符按钮略
private void buttonEqual_Click(object sender, EventArgs e)
{
if (operation == "+")
{
num2 = Convert.ToDouble(textBox1.Text.Trim());
textBox1.Text = Convert.ToString(num1 + num2);
}
else if (operation == "-")
{
num2 = Convert.ToDouble(textBox1.Text.Trim());
textBox1.Text = Convert.ToString(num1 - num2);
}
// 其他运算符逻辑略
flag = true;
}
private void buttonClear_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
num1 = num2 = 0.0;
flag = false;
}
private void buttonPoint_Click(object sender, EventArgs e)
{
if (flag == true)
{
textBox1.Text = "0";
flag = false;
}
if (textBox1.Text.IndexOf('.') == -1)
{
textBox1.Text += ".";
}
}
}
编译并运行程序,可以看到一个简易的计算器界面,可以进行加、减、乘、除的计算。其中,点击清除键(C)可以清空输入和计算结果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现简易计算器功能(2)(窗体应用) - Python技术站