C# 实现的一款简单的计算器
主界面:

代码实现:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 计算器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
internal System.Windows.Forms.Button button2;
internal System.Windows.Forms.Button button3;
internal System.Windows.Forms.Button button4;
internal System.Windows.Forms.Button button5;
internal System.Windows.Forms.Button button6;
internal System.Windows.Forms.Button button7;
internal System.Windows.Forms.Button button8;
internal System.Windows.Forms.Button button9;
internal System.Windows.Forms.Button button10;
internal System.Windows.Forms.Button button11;
internal System.Windows.Forms.Button button12;
internal System.Windows.Forms.Button button13;
internal System.Windows.Forms.Button button14;
internal System.Windows.Forms.Button button15;
internal System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button17;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.textBox1 = new System.Windows.Forms.TextBox();
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.button10 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(128)));
this.textBox1.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.textBox1.ForeColor = System.Drawing.Color.Blue;
this.textBox1.Location = new System.Drawing.Point(24, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 40);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "0";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button1.ForeColor = System.Drawing.Color.Red;
this.button1.Location = new System.Drawing.Point(24, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 1;
this.button1.Text = "1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button2.ForeColor = System.Drawing.Color.Red;
this.button2.Location = new System.Drawing.Point(120, 72);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 23);
this.button2.TabIndex = 2;
this.button2.Text = "2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button3.ForeColor = System.Drawing.Color.Red;
this.button3.Location = new System.Drawing.Point(208, 72);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 23);
this.button3.TabIndex = 3;
this.button3.Text = "3";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button4.ForeColor = System.Drawing.Color.Red;
this.button4.Location = new System.Drawing.Point(24, 112);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(64, 23);
this.button4.TabIndex = 4;
this.button4.Text = "4";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button5.ForeColor = System.Drawing.Color.Red;
this.button5.Location = new System.Drawing.Point(120, 112);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(56, 23);
this.button5.TabIndex = 5;
this.button5.Text = "5";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button6.ForeColor = System.Drawing.Color.Red;
this.button6.Location = new System.Drawing.Point(208, 112);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(56, 23);
this.button6.TabIndex = 6;
this.button6.Text = "6";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button7.ForeColor = System.Drawing.Color.Red;
this.button7.Location = new System.Drawing.Point(24, 152);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(64, 23);
this.button7.TabIndex = 7;
this.button7.Text = "7";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button8
//
this.button8.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button8.ForeColor = System.Drawing.Color.Red;
this.button8.Location = new System.Drawing.Point(120, 152);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(56, 23);
this.button8.TabIndex = 8;
this.button8.Text = "8";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button9
//
this.button9.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button9.ForeColor = System.Drawing.Color.Red;
this.button9.Location = new System.Drawing.Point(208, 152);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(56, 23);
this.button9.TabIndex = 9;
this.button9.Text = "9";
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button10
//
this.button10.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button10.ForeColor = System.Drawing.Color.Red;
this.button10.Location = new System.Drawing.Point(208, 192);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(56, 23);
this.button10.TabIndex = 10;
this.button10.Text = "0";
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// button11
//
this.button11.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(128)));
this.button11.Font = new System.Drawing.Font("宋体", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button11.ForeColor = System.Drawing.Color.Red;
this.button11.Location = new System.Drawing.Point(168, 264);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(96, 64);
this.button11.TabIndex = 11;
this.button11.Text = "=";
this.button11.Click += new System.EventHandler(this.button11_Click);
//
// button12
//
this.button12.BackColor = System.Drawing.Color.Yellow;
this.button12.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button12.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
this.button12.Location = new System.Drawing.Point(64, 208);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(40, 40);
this.button12.TabIndex = 12;
this.button12.Text = "+";
this.button12.Click += new System.EventHandler(this.button12_Click);
//
// button13
//
this.button13.BackColor = System.Drawing.Color.Yellow;
this.button13.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button13.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
this.button13.Location = new System.Drawing.Point(64, 288);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(40, 40);
this.button13.TabIndex = 13;
this.button13.Text = "-";
this.button13.Click += new System.EventHandler(this.button13_Click);
//
// button14
//
this.button14.BackColor = System.Drawing.Color.Yellow;
this.button14.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button14.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
this.button14.Location = new System.Drawing.Point(24, 248);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(40, 40);
this.button14.TabIndex = 14;
this.button14.Text = "*";
this.button14.Click += new System.EventHandler(this.button14_Click);
//
// button15
//
this.button15.BackColor = System.Drawing.Color.Yellow;
this.button15.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button15.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
this.button15.Location = new System.Drawing.Point(104, 248);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(40, 40);
this.button15.TabIndex = 15;
this.button15.Text = "/";
this.button15.Click += new System.EventHandler(this.button15_Click);
//
// button16
//
this.button16.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
this.button16.Location = new System.Drawing.Point(120, 192);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(56, 24);
this.button16.TabIndex = 16;
this.button16.Text = "清空";
this.button16.Click += new System.EventHandler(this.button16_Click);
//
// button17
//
this.button17.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button17.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button17.ForeColor = System.Drawing.Color.Red;
this.button17.Location = new System.Drawing.Point(208, 232);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(56, 23);
this.button17.TabIndex = 17;
this.button17.Text = ".";
this.button17.Click += new System.EventHandler(this.button17_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.Snow;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(288, 333);
this.Controls.Add(this.button17);
this.Controls.Add(this.button16);
this.Controls.Add(this.button15);
this.Controls.Add(this.button14);
this.Controls.Add(this.button13);
this.Controls.Add(this.button12);
this.Controls.Add(this.button11);
this.Controls.Add(this.button10);
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.textBox1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "计算器";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button1.Text;
}
else
{
this.textBox1.Text = this.button1.Text;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text !="0")
{
this.textBox1.Text += this.button2.Text;
}
else
{
this.textBox1.Text = this.button2.Text;
}
}
private void button3_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button3.Text;
}
else
{
this.textBox1.Text = this.button3.Text;
}
}
private void button4_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button4.Text;
}
else
{
this.textBox1.Text = this.button4.Text;
}
}
private void button5_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button5.Text;
}
else
{
this.textBox1.Text = this.button5.Text;
}
}
private void button6_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button6.Text;
}
else
{
this.textBox1.Text = this.button6.Text;
}
}
private void button7_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button7.Text;
}
else
{
this.textBox1.Text = this.button7.Text;
}
}
private void button8_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button8.Text;
}
else
{
this.textBox1.Text = this.button8.Text;
}
}
private void button9_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button9.Text;
}
else
{
this.textBox1.Text = this.button9.Text;
}
}
private void button10_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != "0")
{
this.textBox1.Text += this.button10.Text;
}
else
{
this.textBox1.Text = this.button10.Text;
}
}
private void button11_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text.IndexOf("+") > 0)
{
string[] str = this.textBox1.Text.Split('+');
this.textBox1.Text = (System.Convert.ToDouble(str[0]) + System.Convert.ToDouble(str[1])).ToString();
}
else if (this.textBox1.Text.IndexOf("-") > 0)
{
string[] strr = this.textBox1.Text.Split('-');
this.textBox1.Text = (System.Convert.ToDouble(strr[0]) - System.Convert.ToDouble(strr[1])).ToString();
}
else if (this.textBox1.Text.IndexOf("*") > 0)
{
string[] strrr = this.textBox1.Text.Split('*');
this.textBox1.Text = (System.Convert.ToDouble(strrr[0]) * System.Convert.ToDouble(strrr[1])).ToString();
}
else if (this.textBox1.Text.IndexOf("/") > 0)
{
string[] strrrr = this.textBox1.Text.Split('/');
this.textBox1.Text = (System.Convert.ToDouble(strrrr[0]) / System.Convert.ToDouble(strrrr[1])).ToString();
}
// this.button12.Visible = true;
// this.button13.Visible = true;
// this.button14.Visible = true;
// this.button15.Visible = true;
}
private void button16_Click(object sender, System.EventArgs e)
{
this.textBox1.Text = "0";
this.button12.Visible = true;
this.button13.Visible = true;
this.button14.Visible = true;
this.button15.Visible = true;
}
private void button12_Click(object sender, System.EventArgs e)
{
int i=0;
if(this.textBox1.Text=="0")
{
this.textBox1.Text=this.button12.Text;
}
else
{
this.textBox1.Text+=this.button12.Text;
}
i++;
if(i==1)
{
this.button12.Visible = false;
this.button13.Visible = false;
this.button14.Visible = false;
this.button15.Visible = false;
}
}
private void button14_Click(object sender, System.EventArgs e)
{
int i=0;
if(this.textBox1.Text=="0")
{
this.textBox1.Text=this.button14.Text;
}
else
{
this.textBox1.Text+=this.button14.Text;
}
i++;
if(i==1)
{
this.button14.Visible = false;
this.button12.Visible = false;
this.button13.Visible = false;
this.button15.Visible = false;
}
}
private void button15_Click(object sender, System.EventArgs e)
{
int i=0;
if(this.textBox1.Text=="0")
{
this.textBox1.Text=this.button15.Text;
}
else
{
this.textBox1.Text+=this.button15.Text;
}
i++;
if(i==1)
{
this.button15.Visible = false;
this.button12.Visible = false;
this.button13.Visible = false;
this.button14.Visible = false;
}
}
private void button13_Click(object sender, System.EventArgs e)
{
int i=0;
if(this.textBox1.Text=="0")
{
this.textBox1.Text=this.button13.Text;
}
else
{
this.textBox1.Text+=this.button13.Text;
}
i++;
if(i==1)
{
this.button13.Visible = false;
this.button12.Visible = false;
this.button14.Visible = false;
this.button15.Visible = false;
}
}
private void button17_Click(object sender, System.EventArgs e)
{
if(this.textBox1.Text.Length!=(this.textBox1.Text.IndexOf("*")+1)&&this.textBox1.Text.Length!=((this.textBox1.Text.IndexOf("/")+1))&&this.textBox1.Text.Length!=((this.textBox1.Text.IndexOf("-")+1))&&this.textBox1.Text.Length!=((this.textBox1.Text.IndexOf("+")+1)))
{
this.textBox1.Text+=this.button17.Text;
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
浙公网安备 33010602011771号