1、计算器 2using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace caculatepublic partial class Form1 : Formpublic Form1()InitializeComponent();private void btn_equal_Click(obj
2、ect sender, EventArgs e)/等号secondeValue = Convert.ToDouble(txtBxDisplay.Text);switch (operator1) case “+“: txtBxDisplay.Text = (preValue + secondeValue).ToString(); break;case “-“: txtBxDisplay.Text = (preValue - secondeValue).ToString(); break;case “*“: txtBxDisplay.Text = (preValue * secondeValue)
3、.ToString(); break;case “/“: txtBxDisplay.Text = (preValue / secondeValue).ToString(); break;private void btn_clear_Click(object sender, EventArgs e)/清除-CtxtBxDisplay.Text=“ “;preValue = 0;secondeValue = 0;private void btnNum_Click(object sender, EventArgs e)/处理数字键txtBxDisplay.Text +=(Button)sender)
4、.Text;private void btn_0_Click(object sender, EventArgs e)/0和 00键的处理if (txtBxDisplay.Text = “)txtBxDisplay.Text = “;else if (Button)sender).Text = “0“)txtBxDisplay.Text += “0“;else txtBxDisplay.Text += “00“;private void btn_point_Click(object sender, EventArgs e)/小数点的处理if (txtBxDisplay.Text = “)txtB
5、xDisplay.Text = “0.“;else if (txtBxDisplay.Text.IndexOf('.') / 必需的设计器变量。/ private System.ComponentModel.IContainer components = null;/ / 清理所有正在使用的资源。/ / 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing)if (disposing base.Dispose(disposing);#region Windows 窗体设计器生成的代码/
6、 / 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ private void InitializeComponent()this.txtBxDisplay = new System.Windows.Forms.TextBox();this.btn_2 = new System.Windows.Forms.Button();this.btn_multiply = new System.Windows.Forms.Button();this.btn_plus = new System.Windows.Forms.Button();this.btn_3 = new Syste
7、m.Windows.Forms.Button();this.btn_1 = new System.Windows.Forms.Button();this.btn_7 = new System.Windows.Forms.Button();this.btn_6 = new System.Windows.Forms.Button();this.btn_9 = new System.Windows.Forms.Button();this.btn_point = new System.Windows.Forms.Button();this.btn_8 = new System.Windows.Form
8、s.Button();this.btn_subtract = new System.Windows.Forms.Button();this.btn_0 = new System.Windows.Forms.Button();this.btn_divide = new System.Windows.Forms.Button();this.btn_5 = new System.Windows.Forms.Button();this.btn_4 = new System.Windows.Forms.Button();this.btn_00 = new System.Windows.Forms.But
9、ton();this.btn_equal = new System.Windows.Forms.Button();this.btn_clear = new System.Windows.Forms.Button();this.SuspendLayout();/ / txtBxDisplay/ this.txtBxDisplay.Location = new System.Drawing.Point(12, 27);this.txtBxDisplay.Multiline = true;this.txtBxDisplay.Name = “txtBxDisplay“;this.txtBxDispla
10、y.Size = new System.Drawing.Size(256, 30);this.txtBxDisplay.TabIndex = 0;/ / btn_2/ this.btn_2.Location = new System.Drawing.Point(64, 72);this.btn_2.Name = “btn_2“;this.btn_2.Size = new System.Drawing.Size(46, 23);this.btn_2.TabIndex = 2;this.btn_2.Text = “2“;this.btn_2.UseVisualStyleBackColor = tr
11、ue;this.btn_2.Click += new System.EventHandler(this.btnNum_Click);/ / btn_multiply/ this.btn_multiply.Location = new System.Drawing.Point(222, 72);this.btn_multiply.Name = “btn_multiply“;this.btn_multiply.Size = new System.Drawing.Size(46, 23);this.btn_multiply.TabIndex = 3;this.btn_multiply.Text =
12、“*“;this.btn_multiply.UseVisualStyleBackColor = true;this.btn_multiply.Click += new System.EventHandler(this.btnOperator_Click);/ / btn_plus/ this.btn_plus.Location = new System.Drawing.Point(170, 72);this.btn_plus.Name = “btn_plus“;this.btn_plus.Size = new System.Drawing.Size(46, 23);this.btn_plus.
13、TabIndex = 4;this.btn_plus.Text = “+“;this.btn_plus.UseVisualStyleBackColor = true;this.btn_plus.Click += new System.EventHandler(this.btnOperator_Click);/ / btn_3/ this.btn_3.Location = new System.Drawing.Point(118, 72);this.btn_3.Name = “btn_3“;this.btn_3.Size = new System.Drawing.Size(46, 23);thi
14、s.btn_3.TabIndex = 5;this.btn_3.Text = “3“;this.btn_3.UseVisualStyleBackColor = true;this.btn_3.Click += new System.EventHandler(this.btnNum_Click);/ / btn_1/ this.btn_1.Location = new System.Drawing.Point(12, 72);this.btn_1.Name = “btn_1“;this.btn_1.Size = new System.Drawing.Size(46, 23);this.btn_1
15、.TabIndex = 6;this.btn_1.Text = “1“;this.btn_1.UseVisualStyleBackColor = true;this.btn_1.Click += new System.EventHandler(this.btnNum_Click);/ / btn_7/ this.btn_7.Location = new System.Drawing.Point(12, 133);this.btn_7.Name = “btn_7“;this.btn_7.Size = new System.Drawing.Size(46, 23);this.btn_7.TabIn
16、dex = 7;this.btn_7.Text = “7“;this.btn_7.UseVisualStyleBackColor = true;this.btn_7.Click += new System.EventHandler(this.btnNum_Click);/ / btn_6/ this.btn_6.Location = new System.Drawing.Point(116, 101);this.btn_6.Name = “btn_6“;this.btn_6.Size = new System.Drawing.Size(46, 23);this.btn_6.TabIndex =
17、 8;this.btn_6.Text = “6“;this.btn_6.UseVisualStyleBackColor = true;this.btn_6.Click += new System.EventHandler(this.btnNum_Click);/ / btn_9/ this.btn_9.Location = new System.Drawing.Point(118, 133);this.btn_9.Name = “btn_9“;this.btn_9.Size = new System.Drawing.Size(46, 23);this.btn_9.TabIndex = 9;th
18、is.btn_9.Text = “9“;this.btn_9.UseVisualStyleBackColor = true;this.btn_9.Click += new System.EventHandler(this.btnNum_Click);/ / btn_point/ this.btn_point.Font = new System.Drawing.Font(“宋体“, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (byte)(134);this.btn_point.Locatio
19、n = new System.Drawing.Point(118, 162);this.btn_point.Name = “btn_point“;this.btn_point.Size = new System.Drawing.Size(46, 23);this.btn_point.TabIndex = 10;this.btn_point.Text = “.“;this.btn_point.UseVisualStyleBackColor = true;this.btn_point.Click += new System.EventHandler(this.btn_point_Click);/
20、/ btn_8/ this.btn_8.Location = new System.Drawing.Point(64, 133);this.btn_8.Name = “btn_8“;this.btn_8.Size = new System.Drawing.Size(46, 23);this.btn_8.TabIndex = 11;this.btn_8.Text = “8“;this.btn_8.UseVisualStyleBackColor = true;this.btn_8.Click += new System.EventHandler(this.btnNum_Click);/ / btn
21、_subtract/ this.btn_subtract.Location = new System.Drawing.Point(170, 101);this.btn_subtract.Name = “btn_subtract“;this.btn_subtract.Size = new System.Drawing.Size(46, 23);this.btn_subtract.TabIndex = 12;this.btn_subtract.Text = “-“;this.btn_subtract.UseVisualStyleBackColor = true;this.btn_subtract.
22、Click += new System.EventHandler(this.btnOperator_Click);/ / btn_0/ this.btn_0.Location = new System.Drawing.Point(12, 162);this.btn_0.Name = “btn_0“;this.btn_0.Size = new System.Drawing.Size(46, 23);this.btn_0.TabIndex = 13;this.btn_0.Text = “0“;this.btn_0.UseVisualStyleBackColor = true;this.btn_0.
23、Click += new System.EventHandler(this.btn_0_Click);/ / btn_divide/ this.btn_divide.Location = new System.Drawing.Point(222, 101);this.btn_divide.Name = “btn_divide“;this.btn_divide.Size = new System.Drawing.Size(46, 23);this.btn_divide.TabIndex = 14;this.btn_divide.Text = “/“;this.btn_divide.UseVisu
24、alStyleBackColor = true;this.btn_divide.Click += new System.EventHandler(this.btnOperator_Click);/ / btn_5/ this.btn_5.Location = new System.Drawing.Point(64, 101);this.btn_5.Name = “btn_5“;this.btn_5.Size = new System.Drawing.Size(46, 23);this.btn_5.TabIndex = 15;this.btn_5.Text = “5“;this.btn_5.Us
25、eVisualStyleBackColor = true;this.btn_5.Click += new System.EventHandler(this.btnNum_Click);/ / btn_4/ this.btn_4.Location = new System.Drawing.Point(12, 101);this.btn_4.Name = “btn_4“;this.btn_4.Size = new System.Drawing.Size(46, 23);this.btn_4.TabIndex = 16;this.btn_4.Text = “4“;this.btn_4.UseVisu
26、alStyleBackColor = true;this.btn_4.Click += new System.EventHandler(this.btnNum_Click);/ / btn_00/ this.btn_00.Location = new System.Drawing.Point(64, 162);this.btn_00.Name = “btn_00“;this.btn_00.Size = new System.Drawing.Size(46, 23);this.btn_00.TabIndex = 18;this.btn_00.Text = “00“;this.btn_00.Use
27、VisualStyleBackColor = true;this.btn_00.Click += new System.EventHandler(this.btn_0_Click);/ / btn_equal/ this.btn_equal.Location = new System.Drawing.Point(170, 133);this.btn_equal.Name = “btn_equal“;this.btn_equal.Size = new System.Drawing.Size(98, 23);this.btn_equal.TabIndex = 19;this.btn_equal.T
28、ext = “=“;this.btn_equal.UseVisualStyleBackColor = true;this.btn_equal.Click += new System.EventHandler(this.btn_equal_Click);/ / btn_clear/ this.btn_clear.Location = new System.Drawing.Point(170, 162);this.btn_clear.Name = “btn_clear“;this.btn_clear.Size = new System.Drawing.Size(98, 23);this.btn_c
29、lear.TabIndex = 20;this.btn_clear.Text = “C“;this.btn_clear.UseVisualStyleBackColor = true;this.btn_clear.Click += new System.EventHandler(this.btn_clear_Click);/ / Form1/ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.C
30、lientSize = new System.Drawing.Size(289, 205);this.Controls.Add(this.btn_clear);this.Controls.Add(this.btn_equal);this.Controls.Add(this.btn_00);this.Controls.Add(this.btn_4);this.Controls.Add(this.btn_5);this.Controls.Add(this.btn_divide);this.Controls.Add(this.btn_0);this.Controls.Add(this.btn_sub
31、tract);this.Controls.Add(this.btn_8);this.Controls.Add(this.btn_point);this.Controls.Add(this.btn_9);this.Controls.Add(this.btn_6);this.Controls.Add(this.btn_7);this.Controls.Add(this.btn_1);this.Controls.Add(this.btn_3);this.Controls.Add(this.btn_plus);this.Controls.Add(this.btn_multiply);this.Cont
32、rols.Add(this.btn_2);this.Controls.Add(this.txtBxDisplay);this.Name = “Form1“;this.Text = “Form1“;this.ResumeLayout(false);this.PerformLayout();#endregionprivate System.Windows.Forms.TextBox txtBxDisplay;private System.Windows.Forms.Button btn_2;private System.Windows.Forms.Button btn_multiply;priva
33、te System.Windows.Forms.Button btn_plus;private System.Windows.Forms.Button btn_3;private System.Windows.Forms.Button btn_1;private System.Windows.Forms.Button btn_7;private System.Windows.Forms.Button btn_6;private System.Windows.Forms.Button btn_9;private System.Windows.Forms.Button btn_point;priv
34、ate System.Windows.Forms.Button btn_8;private System.Windows.Forms.Button btn_subtract;private System.Windows.Forms.Button btn_0;private System.Windows.Forms.Button btn_divide;private System.Windows.Forms.Button btn_5;private System.Windows.Forms.Button btn_4;private System.Windows.Forms.Button btn_
35、00;private System.Windows.Forms.Button btn_equal;private System.Windows.Forms.Button btn_clear;Program.cs文件:using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace caculatestatic class Program/ / 应用程序的主入口点。/ STAThreadstatic void Main()Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1();