源代码|分享|自制|WinForm窗体-计算器

应用名称:计算器
代码行数:277行
实现:退格,小数点,运算(加减乘除)
运行截图:
![计算器 2018-11-26 225316.jpg][1]
代码:

using System;
using System.Linq;
using System.Windows.Forms;

namespace WinCalculator
{
public partial class Form1 : Form
{
public bool number = true;//是否按数字才能进行下一步操作
public double StorageTwo = 0;//储存上一次的值
public string operators;//储存运算符
public bool arithmetic = false;//是否进行过运算
public string Equal = “”;//运算的字符=
public string except = “未运行”;//运算的字符/

public Form1()
{
InitializeComponent();//初始化窗体控件
}

private void btn1_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 1;//点击的数字
}

private void btn2_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 2;//点击的数字
}

private void btn3_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 3;//点击的数字
}

private void btn4_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 4;//点击的数字
}

private void btn5_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 5;//点击的数字
}

private void btn6_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 6;//点击的数字
}

private void btn7_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 7;//点击的数字
}

private void btn8_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 8;//点击的数字
}

private void btn9_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 9;//点击的数字
}

private void btn0_Click(object sender, EventArgs e)
{
displays();//以动画形式显示
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += 0;//点击的数字
}

private void btnSpot_Click(object sender, EventArgs e)
{
if (arithmetic == true) { this.txtDisplay.Text = “”; arithmetic = false; }//判断是否运算过,是清空现有值。
this.txtDisplay.Text += “.”;//点击的数字
Use();
}

public void count(Double x, Double y)//运算的方法(传入两个值X,Y)
{
switch (operators)//判断运算符
{
case “+”://如果为+
StorageTwo = x + y;//两个数进行相加
this.txtDisplay.Text = Convert.ToString(StorageTwo);//显示运算值
break;//结束判断
case “-“://如果为-
StorageTwo = x – y;//两个数进行相减
this.txtDisplay.Text = Convert.ToString(StorageTwo);//显示运算值
break;//结束判断
case “*”://如果为*
StorageTwo = x * y;//两个数进行相乘
this.txtDisplay.Text = Convert.ToString(StorageTwo);//显示运算值
break;//结束判断
case “/”://如果为/
if (y != 0)//判断x,y值是否为空
{
StorageTwo = x / y;//两个数进行相除
this.txtDisplay.Text = Convert.ToString(StorageTwo);//显示运算值
}
else
{
MessageBox.Show(“除法含空值,运算不成立”);//如果为空
//清空
number = true;//是否按数字才能进行下一步操作
StorageTwo = 0;//储存上一次的值
operators = “”;//储存运算符
arithmetic = false;//是否进行过运算
Equal = “”;
this.txtDisplay.Text = “”;
//清空
}
break;//结束判断
}
if (Equal == “=”)//如果点击了=
{
Equal = “”;//清除等于运算
this.btnEqual.Enabled = false;//不启用等于号
}
arithmetic = true;//是否运算过
}

private void btnPlus_Click(object sender, EventArgs e)
{
if (StorageTwo == 0)//判断储存的值是否是0
{
StorageTwo = Convert.ToDouble(this.txtDisplay.Text);//把显示的数字赋值到StorageTwo
this.txtDisplay.Text = “”;//重置显示的值
}
else
{
count(StorageTwo, Convert.ToDouble(this.txtDisplay.Text));//赋值到运算的X,Y
}
operators = “+”;//该方法为+
Use();//不启用控件
}

private void btnReduce_Click(object sender, EventArgs e)
{
if (StorageTwo == 0)//判断储存的值是否是0
{
StorageTwo = Convert.ToDouble(this.txtDisplay.Text);//把显示的数字赋值到StorageTwo
this.txtDisplay.Text = “”;//重置显示的值
}
else
{
count(StorageTwo, Convert.ToDouble(this.txtDisplay.Text));//赋值到运算的X,Y
}
operators = “-“;//该方法为-
Use();//不启用控件
}

private void btnRide_Click(object sender, EventArgs e)
{
if (StorageTwo == 0)//判断储存的值是否是0
{
StorageTwo = Convert.ToDouble(this.txtDisplay.Text);//把显示的数字赋值到StorageTwo
this.txtDisplay.Text = “”;//重置显示的值
}
else
{
count(StorageTwo, Convert.ToDouble(this.txtDisplay.Text));//赋值到运算的X,Y
}
operators = “*”;//该方法为*
Use();//不启用控件
}

private void btnExcept_Click(object sender, EventArgs e)
{
if (except != “运行过” && StorageTwo == 0)//判断储存的值是否是0
{
StorageTwo = Convert.ToDouble(this.txtDisplay.Text);//把显示的数字赋值到StorageTwo
this.txtDisplay.Text = “”;//重置显示的值
except = “运行过”;
}
if (this.txtDisplay.Text != “”)//判断储存的值是否是0
{
count(StorageTwo, Convert.ToDouble(this.txtDisplay.Text));//赋值到运算的X,Y
}
operators = “/”;//该方法为/
Use();//不启用控件
}

private void btnAC_Click(object sender, EventArgs e)
{
//到最初的状态
number = true;//
StorageTwo = 0;//
operators = “”;//
arithmetic = false;//
Equal = “”;//
this.txtDisplay.Text = “”;//
except = “未运行”;
}
public void Use()
{
this.btnSpot.Enabled = false;//不启用
this.btnEqual.Enabled = false;//不启用
this.btnPlus.Enabled = false;//不启用
this.btnReduce.Enabled = false;//不启用
this.btnRide.Enabled = false;//不启用
this.btnExcept.Enabled = false;//不启用
}
public void displays()
{
this.btnSpot.Enabled = true;//启用
this.btnEqual.Enabled = true;//启用
this.btnPlus.Enabled = true;//启用
this.btnReduce.Enabled = true;//启用
this.btnRide.Enabled = true;//启用
this.btnExcept.Enabled = true;//启用
}

private void btnEqual_Click(object sender, EventArgs e)
{
if (StorageTwo != 0)//如果不为0
{
Equal = “=”;//点击=
count(StorageTwo, Convert.ToDouble(this.txtDisplay.Text));//赋值到运算的X,Y
}
else
{
if (this.txtDisplay.Text != “”)
{
switch (operators)
{
case “/”:
count(StorageTwo, Convert.ToDouble(this.txtDisplay.Text));
break;
case “*”:
count(StorageTwo, Convert.ToDouble(this.txtDisplay.Text));
break;
}
}
operators = “”;//重置运算符
this.btnSpot.Enabled = false;//不启用
}
}

private void Form1_Load(object sender, EventArgs e)
{
Use();//不启用
}

private void btnDelete_Click(object sender, EventArgs e)
{
if (this.txtDisplay.Text.LongCount() != 0)//判断字符串的数量是否不等于0
{
var Retreat = this.txtDisplay.Text.Remove(Convert.ToInt32(this.txtDisplay.Text.LongCount()) – 1);//获取字符串的字符串的数量-1赋值到Retreat
this.txtDisplay.Text = Retreat;//txtDisplay.Text赋值Retreat
}
}
private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(“https://weimiao.xin/”);
}
}
}

如果有改进之处请留言!
代码下载:[计算器][2]

[1]: https://weimiao.xin/usr/uploads/2018/11/1926102566.jpg
[2]: https://www.lanzous.com/i2gt7aj

0 评论
留言