winform计算器–C
这才学了几节课,啥都不懂呢,跟着视频敲感觉没啥意义。。电脑用vs2019容易特别卡。虽然它长得好看。
.cs
using System;
using System.Windows.Forms;
namespace _201_winform
{
public partial class Form1 : Form
{
private string strOutput="";
private double INumFormer=0;
private double INumTemp=0;
private double IResult=0;
private char cOperator;
private bool bDotClicked=false;//小数点的建是否按下
double lastDecimalNum = 1;//小数的最后以为1.0001
public Form1()
{
InitializeComponent();
EventHandler eh = new EventHandler(Numbers_Click);
button_num0.Click += eh;
button_num1.Click += eh;
button_num1.Click += eh;
button_num3.Click += eh;
button_num4.Click += eh;
button_num5.Click += eh;
button_num6.Click += eh;
button_num7.Click += eh;
button_num8.Click += eh;
button_num9.Click += eh;
EventHandler eh2 = new EventHandler(Operators_Click);
button_add.Click += eh2;
button_sub.Click += eh2;
button_mal.Click += eh2;
button_div.Click += eh2;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
INumFormer = 0;//前一个操作清空
INumTemp = 0;
IResult = 0;
cOperator = '\0';
strOutput = "";
bDotClicked = false;
lastDecimalNum = 1;
txtOutput.Text = "";//页面显示情kong
}
private void button_div_Click(object sender, EventArgs e)
{
//除法的实现,统一使用操作符实现
}
private void button_mal_Click(object sender, EventArgs e)
{
//乘法的实现,统一使用操作符
}
private void button_sub_Click(object sender, EventArgs e)
{
//减法的实现
}
private void button_add_Click(object sender, EventArgs e)
{
//加法的实现
}
private void button_num7_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num8_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num9_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num4_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num5_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num6_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num1_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num2_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num3_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_dot_Click(object sender, EventArgs e)
{
if(!bDotClicked)
{
strOutput += ".";
txtOutput.Text = strOutput;
bDotClicked = true;
}
}
private void button_enter_Click(object sender, EventArgs e)
{
try
{
switch (cOperator)
{
case '+':
checked
{
IResult = INumFormer + INumTemp;
}
break;
case '-':
checked
{
IResult = INumFormer - INumTemp;
}
break;
case '*':
checked
{
IResult = INumFormer * INumTemp;
}
break;
case '/':
checked
{
IResult = INumFormer / INumTemp;
}
break;
default:
break;
}
}
catch (Exception)
{
MessageBox.Show("计算结果溢出");
}
txtOutput.Text = IResult.ToString();
INumFormer = 0;
INumTemp = 0;
IResult = 0;
cOperator = '\0';
bDotClicked = false;
lastDecimalNum = 1;
}
//集中处理按钮点击事件
private void Numbers_Click(object senders,EventArgs e)
{
string strClickNum = ((Button)senders).Text;
try
{
if(bDotClicked)
{
lastDecimalNum *= 0.1;
//得出点击的小数数值
checked
{
INumTemp = INumTemp + long.Parse(strClickNum) * lastDecimalNum;
}
}
else
{
checked
{
INumTemp = INumTemp * 10 + long.Parse(strClickNum);
}
}
}
catch (Exception)
{
MessageBox.Show("数据溢出");
}
txtOutput.Text = INumTemp.ToString();
strOutput += strClickNum;
txtOutput.Text = strOutput;
}
//操作符按钮点击事件
private void Operators_Click(object senders, EventArgs e)
{
string strClickOp = ((Button)senders).Text;
cOperator = strClickOp[0];
INumFormer = INumTemp;
INumTemp = 0;
strOutput += cOperator.ToString();
txtOutput.Text = strOutput;
bDotClicked = false;
lastDecimalNum = 1;
}
protected override bool ProcessCmdKey(ref Message msg,Keys keyData)
{
switch (keyData)
{
case Keys.NumPad0:
button_num0.Focus();
button_num0.PerformClick();
return true;
case Keys.NumPad1:
button_num1.Focus();
button_num1.PerformClick();
return true;
case Keys.NumPad2:
button_num2.Focus();
button_num2.PerformClick();
return true;
case Keys.NumPad3:
button_num3.Focus();
button_num3.PerformClick();
return true;
case Keys.NumPad4:
button_num4.Focus();
button_num4.PerformClick();
return true;
case Keys.NumPad5:
button_num5.Focus();
button_num5.PerformClick();
return true;
case Keys.NumPad6:
button_num6.Focus();
button_num6.PerformClick();
return true;
case Keys.NumPad7:
button_num7.Focus();
button_num7.PerformClick();
return true;
case Keys.NumPad8:
button_num8.Focus();
button_num8.PerformClick();
return true;
case Keys.NumPad9:
button_num9.Focus();
button_num9.PerformClick();
return true;
case Keys.Add:
button_add.Focus();
button_add.PerformClick();
return true;
case Keys.Subtract:
button_sub.Focus();
button_sub.PerformClick();
return true;
case Keys.Multiply:
button_mal.Focus();
button_mal.PerformClick();
return true;
case Keys.Divide:
button_div.Focus();
button_div.PerformClick();
return true;
case Keys.Enter:
button_enter.Focus();
button_enter.PerformClick();
return true;
case Keys.Escape:
button_CE.Focus();
button_CE.PerformClick();
return true;
case Keys.Decimal:
button_dot.Focus();
button_dot.PerformClick();
return true;
default:
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}
.designer.cs
namespace _201_winform
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtOutput = new System.Windows.Forms.TextBox();
this.button_CE = new System.Windows.Forms.Button();
this.button_div = new System.Windows.Forms.Button();
this.button_mal = new System.Windows.Forms.Button();
this.button_sub = new System.Windows.Forms.Button();
this.button_num7 = new System.Windows.Forms.Button();
this.button_num8 = new System.Windows.Forms.Button();
this.button_num9 = new System.Windows.Forms.Button();
this.button_num4 = new System.Windows.Forms.Button();
this.button_num6 = new System.Windows.Forms.Button();
this.button_num5 = new System.Windows.Forms.Button();
this.button_num3 = new System.Windows.Forms.Button();
this.button_num2 = new System.Windows.Forms.Button();
this.button_num1 = new System.Windows.Forms.Button();
this.button_dot = new System.Windows.Forms.Button();
this.button_num0 = new System.Windows.Forms.Button();
this.button_add = new System.Windows.Forms.Button();
this.button_enter = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtOutput
//
this.txtOutput.Location = new System.Drawing.Point(-3, 48);
this.txtOutput.Margin = new System.Windows.Forms.Padding(5, 6, 5, 6);
this.txtOutput.Multiline = true;
this.txtOutput.Name = "txtOutput";
this.txtOutput.Size = new System.Drawing.Size(387, 48);
this.txtOutput.TabIndex = 0;
//
// button_CE
//
this.button_CE.Font = new System.Drawing.Font("微软雅黑", 14F);
this.button_CE.Location = new System.Drawing.Point(-3, 105);
this.button_CE.Name = "button_CE";
this.button_CE.Size = new System.Drawing.Size(87, 47);
this.button_CE.TabIndex = 1;
this.button_CE.Text = "CE";
this.button_CE.UseVisualStyleBackColor = true;
this.button_CE.Click += new System.EventHandler(this.button1_Click);
//
// button_div
//
this.button_div.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_div.Location = new System.Drawing.Point(99, 105);
this.button_div.Name = "button_div";
this.button_div.Size = new System.Drawing.Size(87, 47);
this.button_div.TabIndex = 2;
this.button_div.Text = "\\";
this.button_div.UseVisualStyleBackColor = true;
this.button_div.Click += new System.EventHandler(this.button_div_Click);
//
// button_mal
//
this.button_mal.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_mal.Location = new System.Drawing.Point(195, 105);
this.button_mal.Name = "button_mal";
this.button_mal.Size = new System.Drawing.Size(87, 47);
this.button_mal.TabIndex = 3;
this.button_mal.Text = "*";
this.button_mal.UseVisualStyleBackColor = true;
this.button_mal.Click += new System.EventHandler(this.button_mal_Click);
//
// button_sub
//
this.button_sub.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_sub.Location = new System.Drawing.Point(297, 105);
this.button_sub.Name = "button_sub";
this.button_sub.Size = new System.Drawing.Size(87, 47);
this.button_sub.TabIndex = 4;
this.button_sub.Text = "-";
this.button_sub.UseVisualStyleBackColor = true;
this.button_sub.Click += new System.EventHandler(this.button_sub_Click);
//
// button_num7
//
this.button_num7.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num7.Location = new System.Drawing.Point(-3, 169);
this.button_num7.Name = "button_num7";
this.button_num7.Size = new System.Drawing.Size(87, 47);
this.button_num7.TabIndex = 5;
this.button_num7.Text = "7";
this.button_num7.UseVisualStyleBackColor = true;
this.button_num7.Click += new System.EventHandler(this.button_num7_Click);
//
// button_num8
//
this.button_num8.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num8.Location = new System.Drawing.Point(99, 169);
this.button_num8.Name = "button_num8";
this.button_num8.Size = new System.Drawing.Size(87, 47);
this.button_num8.TabIndex = 6;
this.button_num8.Text = "8";
this.button_num8.UseVisualStyleBackColor = true;
this.button_num8.Click += new System.EventHandler(this.button_num8_Click);
//
// button_num9
//
this.button_num9.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num9.Location = new System.Drawing.Point(195, 169);
this.button_num9.Name = "button_num9";
this.button_num9.Size = new System.Drawing.Size(87, 47);
this.button_num9.TabIndex = 7;
this.button_num9.Text = "9";
this.button_num9.UseVisualStyleBackColor = true;
this.button_num9.Click += new System.EventHandler(this.button_num9_Click);
//
// button_num4
//
this.button_num4.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num4.Location = new System.Drawing.Point(-3, 245);
this.button_num4.Name = "button_num4";
this.button_num4.Size = new System.Drawing.Size(87, 47);
this.button_num4.TabIndex = 8;
this.button_num4.Text = "4";
this.button_num4.UseVisualStyleBackColor = true;
this.button_num4.Click += new System.EventHandler(this.button_num4_Click);
//
// button_num6
//
this.button_num6.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num6.Location = new System.Drawing.Point(195, 245);
this.button_num6.Name = "button_num6";
this.button_num6.Size = new System.Drawing.Size(87, 47);
this.button_num6.TabIndex = 9;
this.button_num6.Text = "6";
this.button_num6.UseVisualStyleBackColor = true;
this.button_num6.Click += new System.EventHandler(this.button_num6_Click);
//
// button_num5
//
this.button_num5.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num5.Location = new System.Drawing.Point(99, 245);
this.button_num5.Name = "button_num5";
this.button_num5.Size = new System.Drawing.Size(87, 47);
this.button_num5.TabIndex = 10;
this.button_num5.Text = "5";
this.button_num5.UseVisualStyleBackColor = true;
this.button_num5.Click += new System.EventHandler(this.button_num5_Click);
//
// button_num3
//
this.button_num3.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num3.Location = new System.Drawing.Point(195, 316);
this.button_num3.Name = "button_num3";
this.button_num3.Size = new System.Drawing.Size(87, 47);
this.button_num3.TabIndex = 11;
this.button_num3.Text = "3";
this.button_num3.UseVisualStyleBackColor = true;
this.button_num3.Click += new System.EventHandler(this.button_num3_Click);
//
// button_num2
//
this.button_num2.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num2.Location = new System.Drawing.Point(99, 316);
this.button_num2.Name = "button_num2";
this.button_num2.Size = new System.Drawing.Size(87, 47);
this.button_num2.TabIndex = 12;
this.button_num2.Text = "2";
this.button_num2.UseVisualStyleBackColor = true;
this.button_num2.Click += new System.EventHandler(this.button_num2_Click);
//
// button_num1
//
this.button_num1.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num1.Location = new System.Drawing.Point(-3, 316);
this.button_num1.Name = "button_num1";
this.button_num1.Size = new System.Drawing.Size(87, 47);
this.button_num1.TabIndex = 13;
this.button_num1.Text = "1";
this.button_num1.UseVisualStyleBackColor = true;
this.button_num1.Click += new System.EventHandler(this.button_num1_Click);
//
// button_dot
//
this.button_dot.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_dot.Location = new System.Drawing.Point(195, 379);
this.button_dot.Name = "button_dot";
this.button_dot.Size = new System.Drawing.Size(87, 47);
this.button_dot.TabIndex = 14;
this.button_dot.Text = ".";
this.button_dot.UseVisualStyleBackColor = true;
this.button_dot.Click += new System.EventHandler(this.button_dot_Click);
//
// button_num0
//
this.button_num0.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_num0.Location = new System.Drawing.Point(12, 379);
this.button_num0.Name = "button_num0";
this.button_num0.Size = new System.Drawing.Size(169, 47);
this.button_num0.TabIndex = 15;
this.button_num0.Text = "0";
this.button_num0.UseVisualStyleBackColor = true;
//
// button_add
//
this.button_add.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_add.Location = new System.Drawing.Point(299, 169);
this.button_add.Name = "button_add";
this.button_add.Size = new System.Drawing.Size(85, 123);
this.button_add.TabIndex = 16;
this.button_add.Text = "+";
this.button_add.UseVisualStyleBackColor = true;
this.button_add.Click += new System.EventHandler(this.button_add_Click);
//
// button_enter
//
this.button_enter.Font = new System.Drawing.Font("微软雅黑", 14.2F);
this.button_enter.Location = new System.Drawing.Point(299, 316);
this.button_enter.Name = "button_enter";
this.button_enter.Size = new System.Drawing.Size(85, 110);
this.button_enter.TabIndex = 17;
this.button_enter.Text = "=";
this.button_enter.UseVisualStyleBackColor = true;
this.button_enter.Click += new System.EventHandler(this.button_enter_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(14F, 30F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(385, 508);
this.Controls.Add(this.button_enter);
this.Controls.Add(this.button_add);
this.Controls.Add(this.button_num0);
this.Controls.Add(this.button_dot);
this.Controls.Add(this.button_num1);
this.Controls.Add(this.button_num2);
this.Controls.Add(this.button_num3);
this.Controls.Add(this.button_num5);
this.Controls.Add(this.button_num6);
this.Controls.Add(this.button_num4);
this.Controls.Add(this.button_num9);
this.Controls.Add(this.button_num8);
this.Controls.Add(this.button_num7);
this.Controls.Add(this.button_sub);
this.Controls.Add(this.button_mal);
this.Controls.Add(this.button_div);
this.Controls.Add(this.button_CE);
this.Controls.Add(this.txtOutput);
this.Font = new System.Drawing.Font("微软雅黑", 13.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.Margin = new System.Windows.Forms.Padding(5, 6, 5, 6);
this.Name = "Form1";
this.Text = "计算器";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtOutput;
private System.Windows.Forms.Button button_CE;
private System.Windows.Forms.Button button_div;
private System.Windows.Forms.Button button_mal;
private System.Windows.Forms.Button button_sub;
private System.Windows.Forms.Button button_num7;
private System.Windows.Forms.Button button_num8;
private System.Windows.Forms.Button button_num9;
private System.Windows.Forms.Button button_num4;
private System.Windows.Forms.Button button_num6;
private System.Windows.Forms.Button button_num5;
private System.Windows.Forms.Button button_num3;
private System.Windows.Forms.Button button_num2;
private System.Windows.Forms.Button button_num1;
private System.Windows.Forms.Button button_dot;
private System.Windows.Forms.Button button_num0;
private System.Windows.Forms.Button button_add;
private System.Windows.Forms.Button button_enter;
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _201_winform
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}