C#编写简易计算器(附源代码)超详细_第1页
C#编写简易计算器(附源代码)超详细_第2页
C#编写简易计算器(附源代码)超详细_第3页
C#编写简易计算器(附源代码)超详细_第4页
C#编写简易计算器(附源代码)超详细_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、超详细一、 因为计算器设计的控件太多,不便使用控制台应用程序完成,所以这里使用Windows窗体应用程序,并命名为Calc,如下图所示:二、 向窗体中拖入需要的控件,如下图所示:(完成效果图)结果显示区(作者博客左边的文本框)是TextBox控件,并修改其name为txtShow ,按键09为Button控件,并将其name分别修改为btn_0、btn_1、btn_2、btn_3、btn_4、btn_5、btn_6、btn_7、btn_8、btn_9;按键【负数】的name值修改为btn_sign,按键【.】的name修改为btn_dot,按键【+ - * /】的name值分别修改为btn_a

2、dd、btn_sub、btn_mul、btn_div,按键【=】的name值修改为btn_equ,按键【倒数】的name值修改为btn_rev,按键【平方】的name值修改为btn_sqr,按键【开方】的name值修改为btn_sqrt。右边的计算器图片空间是PictureBox,作者博客控件是LinkLabel,可以不添加,以上所有控件均可按照需求添加,只保留自己需要的按钮控件和textbox控件即可。三、 代码部分(含解释),采用switch多分支语句编写using System;using System.Drawing;using System.Collections;using Sys

3、tem.ComponentModel;using System.Windows.Forms;using System.Data;namespace Calc/ / QQ: 温柔一刀C#简易计算器的实现/ public class CalcForm : System.Windows.Forms.Formprivate System.Windows.Forms.Button btn_0;private System.Windows.Forms.Button btn_1;private System.Windows.Forms.Button btn_2;private System.Windows.

4、Forms.Button btn_3;private System.Windows.Forms.Button btn_4;private System.Windows.Forms.Button btn_5;private System.Windows.Forms.Button btn_6;private System.Windows.Forms.Button btn_7;private System.Windows.Forms.Button btn_8;private System.Windows.Forms.Button btn_9;private System.Windows.Forms.

5、Button btn_add;private System.Windows.Forms.Button btn_sub;private System.Windows.Forms.Button btn_mul;private System.Windows.Forms.Button btn_div;private System.Windows.Forms.Button btn_sqrt;private System.Windows.Forms.Button btn_sign;private System.Windows.Forms.Button btn_equ;private System.Wind

6、ows.Forms.Button btn_dot;private System.Windows.Forms.Button btn_rev;private System.Windows.Forms.TextBox txtShow;private System.Windows.Forms.Button btn_sqr; private PictureBox pictureBox1; private LinkLabel linkLabel1;/ / 必需的设计器变量。/ private System.ComponentModel.Container components = null;public

7、CalcForm()/ Windows 窗体设计器支持所必需的/InitializeComponent();/ TODO: 在InitializeComponent 调用后添加任何构造函数代码/ / 清理所有正在使用的资源。/ protected override void Dispose( bool disposing )if( disposing )if (components != null) components.Dispose();base.Dispose( disposing );#region Windows Form Designer generated code/ / 设计器

8、支持所需的方法- 不要使用代码编辑器修改/ 此方法的内容。/ private void InitializeComponent() System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CalcForm); this.btn_9 = new System.Windows.Forms.Button(); this.txtShow = new System.Windows.Forms.TextBox(); this.bt

9、n_8 = new System.Windows.Forms.Button(); this.btn_7 = new System.Windows.Forms.Button(); this.btn_div = new System.Windows.Forms.Button(); this.btn_sqrt = new System.Windows.Forms.Button(); this.btn_4 = new System.Windows.Forms.Button(); this.btn_5 = new System.Windows.Forms.Button(); this.btn_6 = n

10、ew System.Windows.Forms.Button(); this.btn_1 = new System.Windows.Forms.Button(); this.btn_2 = new System.Windows.Forms.Button(); this.btn_3 = new System.Windows.Forms.Button(); this.btn_0 = new System.Windows.Forms.Button(); this.btn_mul = new System.Windows.Forms.Button(); this.btn_sub = new Syste

11、m.Windows.Forms.Button(); this.btn_sign = new System.Windows.Forms.Button(); this.btn_equ = new System.Windows.Forms.Button(); this.btn_add = new System.Windows.Forms.Button(); this.btn_dot = new System.Windows.Forms.Button(); this.btn_sqr = new System.Windows.Forms.Button(); this.btn_rev = new Syst

12、em.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.linkLabel1 = new System.Windows.Forms.LinkLabel(); (System.ComponentModel.ISupportInitialize)(this.pictureBox1).BeginInit(); this.SuspendLayout(); / / btn_9 / this.btn_9.BackColor = System.Drawing.SystemColors.

13、ActiveBorder; this.btn_9.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_9.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_9.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_9.Font = new System.Drawing.Font(黑

14、体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_9.ForeColor = System.Drawing.Color.Black; this.btn_9.Location = new System.Drawing.Point(126, 37); this.btn_9.Name = btn_9; this.btn_9.Size = new System.Drawing.Size(59, 31); this.btn_9.TabIndex = 0; th

15、is.btn_9.Tag = 9; this.btn_9.Text = 9; this.btn_9.UseVisualStyleBackColor = false; this.btn_9.Click += new System.EventHandler(this.btn_0_Click); / / txtShow / this.txtShow.Location = new System.Drawing.Point(4, 6); this.txtShow.Name = txtShow; this.txtShow.ReadOnly = true; this.txtShow.Size = new S

16、ystem.Drawing.Size(242, 21); this.txtShow.TabIndex = 1; this.txtShow.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; / / btn_8 / this.btn_8.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_8.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_8.FlatApp

17、earance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_8.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_8.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_8.ForeColo

18、r = System.Drawing.Color.Black; this.btn_8.Location = new System.Drawing.Point(66, 37); this.btn_8.Name = btn_8; this.btn_8.Size = new System.Drawing.Size(59, 31); this.btn_8.TabIndex = 2; this.btn_8.Tag = 8; this.btn_8.Text = 8; this.btn_8.UseVisualStyleBackColor = false; this.btn_8.Click += new Sy

19、stem.EventHandler(this.btn_0_Click); / / btn_7 / this.btn_7.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_7.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_7.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_7.FlatAppearan

20、ce.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_7.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_7.ForeColor = System.Drawing.Color.Black; this.btn_7.Location = new System.Drawing.Point(4, 37); this.

21、btn_7.Name = btn_7; this.btn_7.Size = new System.Drawing.Size(59, 31); this.btn_7.TabIndex = 3; this.btn_7.Tag = 7; this.btn_7.Text = 7; this.btn_7.UseVisualStyleBackColor = false; this.btn_7.Click += new System.EventHandler(this.btn_0_Click); / / btn_div / this.btn_div.BackColor = System.Drawing.Sy

22、stemColors.ActiveBorder; this.btn_div.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_div.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_div.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_div.Font = new Sy

23、stem.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_div.ForeColor = System.Drawing.Color.Red; this.btn_div.Location = new System.Drawing.Point(187, 37); this.btn_div.Name = btn_div; this.btn_div.Size = new System.Drawing.Size(59, 31);

24、this.btn_div.TabIndex = 4; this.btn_div.Text = /; this.btn_div.UseVisualStyleBackColor = false; this.btn_div.Click += new System.EventHandler(this.btn_div_Click); / / btn_sqrt / this.btn_sqrt.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_sqrt.BackgroundImageLayout = System.Windows.F

25、orms.ImageLayout.Center; this.btn_sqrt.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_sqrt.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_sqrt.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.

26、GraphicsUnit.Point, (byte)(134); this.btn_sqrt.ForeColor = System.Drawing.Color.Black; this.btn_sqrt.Location = new System.Drawing.Point(253, 37); this.btn_sqrt.Name = btn_sqrt; this.btn_sqrt.Size = new System.Drawing.Size(59, 31); this.btn_sqrt.TabIndex = 5; this.btn_sqrt.Text = 开方; this.btn_sqrt.U

27、seVisualStyleBackColor = false; this.btn_sqrt.Click += new System.EventHandler(this.btn_sqrt_Click); / / btn_4 / this.btn_4.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_4.FlatAppearance.MouseDownBackColor =

28、 System.Drawing.SystemColors.ButtonFace; this.btn_4.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_4.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_4.ForeColor = System.Drawing.Color.Bla

29、ck; this.btn_4.Location = new System.Drawing.Point(4, 72); this.btn_4.Name = btn_4; this.btn_4.Size = new System.Drawing.Size(59, 31); this.btn_4.TabIndex = 6; this.btn_4.Tag = 4; this.btn_4.Text = 4; this.btn_4.UseVisualStyleBackColor = false; this.btn_4.Click += new System.EventHandler(this.btn_0_

30、Click); / / btn_5 / this.btn_5.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_5.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_5.FlatAppearance.MouseOverBackColor = Syste

31、m.Drawing.Color.WhiteSmoke; this.btn_5.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_5.ForeColor = System.Drawing.Color.Black; this.btn_5.Location = new System.Drawing.Point(66, 72); this.btn_5.Name = btn_5; this.btn

32、_5.Size = new System.Drawing.Size(59, 31); this.btn_5.TabIndex = 7; this.btn_5.Tag = 5; this.btn_5.Text = 5; this.btn_5.UseVisualStyleBackColor = false; this.btn_5.Click += new System.EventHandler(this.btn_0_Click); / / btn_6 / this.btn_6.BackColor = System.Drawing.SystemColors.ActiveBorder; this.bt

33、n_6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_6.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_6.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_6.Font = new System.Drawing.Font(黑体, 10.5F, System.Draw

34、ing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_6.ForeColor = System.Drawing.Color.Black; this.btn_6.Location = new System.Drawing.Point(126, 72); this.btn_6.Name = btn_6; this.btn_6.Size = new System.Drawing.Size(59, 31); this.btn_6.TabIndex = 8; this.btn_6.Tag = 6; thi

35、s.btn_6.Text = 6; this.btn_6.UseVisualStyleBackColor = false; this.btn_6.Click += new System.EventHandler(this.btn_0_Click); / / btn_1 / this.btn_1.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_1.FlatAppeara

36、nce.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_1.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_1.ForeColor =

37、System.Drawing.Color.Black; this.btn_1.Location = new System.Drawing.Point(4, 108); this.btn_1.Name = btn_1; this.btn_1.Size = new System.Drawing.Size(59, 31); this.btn_1.TabIndex = 9; this.btn_1.Tag = 1; this.btn_1.Text = 1; this.btn_1.UseVisualStyleBackColor = false; this.btn_1.Click += new System

38、.EventHandler(this.btn_0_Click); / / btn_2 / this.btn_2.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_2.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_2.FlatAppearance.M

39、ouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_2.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_2.ForeColor = System.Drawing.Color.Black; this.btn_2.Location = new System.Drawing.Point(66, 108); this.bt

40、n_2.Name = btn_2; this.btn_2.Size = new System.Drawing.Size(59, 31); this.btn_2.TabIndex = 10; this.btn_2.Tag = 2; this.btn_2.Text = 2; this.btn_2.UseVisualStyleBackColor = false; this.btn_2.Click += new System.EventHandler(this.btn_0_Click); / / btn_3 / this.btn_3.BackColor = System.Drawing.SystemC

41、olors.ActiveBorder; this.btn_3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_3.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_3.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_3.Font = new System.Drawing.

42、Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_3.ForeColor = System.Drawing.Color.Black; this.btn_3.Location = new System.Drawing.Point(126, 108); this.btn_3.Name = btn_3; this.btn_3.Size = new System.Drawing.Size(59, 31); this.btn_3.TabIndex

43、= 11; this.btn_3.Tag = 3; this.btn_3.Text = 3; this.btn_3.UseVisualStyleBackColor = false; this.btn_3.Click += new System.EventHandler(this.btn_0_Click); / / btn_0 / this.btn_0.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_0.BackgroundImageLayout = System.Windows.Forms.ImageLayout.C

44、enter; this.btn_0.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace; this.btn_0.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_0.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(

45、134); this.btn_0.ForeColor = System.Drawing.Color.Black; this.btn_0.Location = new System.Drawing.Point(4, 143); this.btn_0.Name = btn_0; this.btn_0.Size = new System.Drawing.Size(59, 31); this.btn_0.TabIndex = 12; this.btn_0.Tag = 0; this.btn_0.Text = 0; this.btn_0.UseVisualStyleBackColor = false;

46、this.btn_0.Click += new System.EventHandler(this.btn_0_Click); / / btn_mul / this.btn_mul.BackColor = System.Drawing.SystemColors.ActiveBorder; this.btn_mul.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.btn_mul.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.B

47、uttonFace; this.btn_mul.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke; this.btn_mul.Font = new System.Drawing.Font(黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte)(134); this.btn_mul.ForeColor = System.Drawing.Color.Red; this.btn_mul.Location = new System.Drawing.Point(187, 72

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论