calculator based on C# Winform

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
Link to the finished project codegithub
The Aim of This AssignmentCreate a calculator with a visual interface
MU STU ID and FZU STU ID<21125210_832102106>

I. PSP form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning(total)500700
• Estimate500700
Development(total)360440
• Analysis5070
• Design Spec1520
• Design Review1520
• Coding Standard1520
• Design5060
• Coding125140
• Code Review5060
• Test4050
Reporting(total)120120
• Test Repor1010
• Size Measurement1010
• Postmortem & Process Improvement Plan100100
Sum9801260

II. Introduction

In this blog I will describe in detail the calculator based on C# Winform implementation.

This calculator I created can realize the function of addition, subtraction, multiplication, division, clearing, exponentiation, logarithm and trigonometric function.

Link to the finished project code:Link

III. Ideas before making a project

About Winform

Windows Forms, often abbreviated as WinForms, is a graphical user interface (GUI) framework for building Windows desktop applications in the Microsoft .NET Framework. It provides developers with a set of tools and libraries for creating interactive and visually appealing desktop applications for the Windows operating system.

Why Winform

  1. Rapid Development: WinForms provides a visual designer that simplifies the process of designing the calculator’s user interface. You can quickly drag and drop buttons, textboxes, and other controls, making it easy to create the calculator’s layout.
  2. Event-Driven Model: Calculators are inherently event-driven applications, as they respond to user input such as button clicks and keyboard presses. WinForms’ event-driven programming model is well-suited for handling these user interactions.
  3. Rich Control Library: WinForms offers a rich set of controls, including buttons, labels, and textboxes, which are essential for building a calculator’s user interface. These controls are highly customizable, allowing you to create a visually appealing calculator.

IV. Design and implementation process

1. Create a New WinForms Project

Start by creating a new WinForms project using a development environment like Microsoft Visual Studio(I use the Visual Studio 2012). This project will serve as the foundation for the calculator application.

2. Design the User Interface (UI):

Static Interface Design

  • Use the WinForms designer to create the calculator’s user interface. I add buttons for numbers (0-9), arithmetic operations (+, -, *, /, ^, log, sin, cos, tan), and a label to display the input and results.
  • Arrange and align the controls on the form to resemble a calculator layout.
  • I optimise the calculator interface by setting the properties of the controls. Change the name and txt of the controls for subsequent design.
  • I also add a picture to the BackgroundImage of the Form and set the BackgroundImageLayout as Stretch to make image adapts to the size of the Form.
    在这里插入图片描述
  • In order to make the buttons transparent makes the Form image visible, I set the BackColor as Transparent, and the FlatStyle as Flat.

在这里插入图片描述
The final result is shown below:
在这里插入图片描述

Dynamic Interface Design

I set the parameters for the mouse event in the button so that the colour changes when the mouse passes over the button.
在这里插入图片描述
The effect is as below:

在这里插入图片描述

The code of Form.Designer.cs:

namespace calculator
{
    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.lblShow = new System.Windows.Forms.Label();
            this.btn1 = new System.Windows.Forms.Button();
            this.btn2 = new System.Windows.Forms.Button();
            this.btn3 = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.btn4 = new System.Windows.Forms.Button();
            this.btn5 = new System.Windows.Forms.Button();
            this.btn6 = new System.Windows.Forms.Button();
            this.btnSub = new System.Windows.Forms.Button();
            this.btn7 = new System.Windows.Forms.Button();
            this.btn8 = new System.Windows.Forms.Button();
            this.btn9 = new System.Windows.Forms.Button();
            this.btnMul = new System.Windows.Forms.Button();
            this.btnDot = new System.Windows.Forms.Button();
            this.btn0 = new System.Windows.Forms.Button();
            this.btnDiv = new System.Windows.Forms.Button();
            this.btnEqual = new System.Windows.Forms.Button();
            this.btnAC = new System.Windows.Forms.Button();
            this.btncos = new System.Windows.Forms.Button();
            this.btnsin = new System.Windows.Forms.Button();
            this.btnlog = new System.Windows.Forms.Button();
            this.btnex = new System.Windows.Forms.Button();
            this.btntan = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // lblShow
            // 
            this.lblShow.BackColor = System.Drawing.Color.Transparent;
            this.lblShow.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.lblShow.Font = new System.Drawing.Font("宋体", 30F);
            this.lblShow.ForeColor = System.Drawing.Color.White;
            this.lblShow.Location = new System.Drawing.Point(28, 23);
            this.lblShow.Name = "lblShow";
            this.lblShow.Size = new System.Drawing.Size(555, 126);
            this.lblShow.TabIndex = 0;
            this.lblShow.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // btn1
            // 
            this.btn1.BackColor = System.Drawing.Color.Transparent;
            this.btn1.FlatAppearance.BorderColor = System.Drawing.Color.White;
            this.btn1.FlatAppearance.BorderSize = 3;
            this.btn1.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn1.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn1.ForeColor = System.Drawing.Color.White;
            this.btn1.Location = new System.Drawing.Point(28, 193);
            this.btn1.Name = "btn1";
            this.btn1.Size = new System.Drawing.Size(90, 47);
            this.btn1.TabIndex = 1;
            this.btn1.Text = "1";
            this.btn1.UseVisualStyleBackColor = false;
            this.btn1.Click += new System.EventHandler(this.btn1_Click);
            // 
            // btn2
            // 
            this.btn2.BackColor = System.Drawing.Color.Transparent;
            this.btn2.FlatAppearance.BorderSize = 3;
            this.btn2.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn2.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn2.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn2.ForeColor = System.Drawing.Color.White;
            this.btn2.Location = new System.Drawing.Point(144, 193);
            this.btn2.Name = "btn2";
            this.btn2.Size = new System.Drawing.Size(90, 47);
            this.btn2.TabIndex = 1;
            this.btn2.Text = "2";
            this.btn2.UseVisualStyleBackColor = false;
            this.btn2.Click += new System.EventHandler(this.btn2_Click);
            // 
            // btn3
            // 
            this.btn3.BackColor = System.Drawing.Color.Transparent;
            this.btn3.FlatAppearance.BorderSize = 3;
            this.btn3.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn3.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn3.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn3.ForeColor = System.Drawing.Color.White;
            this.btn3.Location = new System.Drawing.Point(260, 193);
            this.btn3.Name = "btn3";
            this.btn3.Size = new System.Drawing.Size(90, 47);
            this.btn3.TabIndex = 1;
            this.btn3.Text = "3";
            this.btn3.UseVisualStyleBackColor = false;
            this.btn3.Click += new System.EventHandler(this.btn3_Click);
            // 
            // btnAdd
            // 
            this.btnAdd.BackColor = System.Drawing.Color.Transparent;
            this.btnAdd.FlatAppearance.BorderSize = 3;
            this.btnAdd.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnAdd.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnAdd.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold);
            this.btnAdd.ForeColor = System.Drawing.Color.White;
            this.btnAdd.Location = new System.Drawing.Point(376, 193);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(90, 47);
            this.btnAdd.TabIndex = 1;
            this.btnAdd.Text = "+";
            this.btnAdd.UseVisualStyleBackColor = false;
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // btn4
            // 
            this.btn4.BackColor = System.Drawing.Color.Transparent;
            this.btn4.FlatAppearance.BorderSize = 3;
            this.btn4.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn4.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn4.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn4.ForeColor = System.Drawing.Color.White;
            this.btn4.Location = new System.Drawing.Point(28, 276);
            this.btn4.Name = "btn4";
            this.btn4.Size = new System.Drawing.Size(90, 47);
            this.btn4.TabIndex = 1;
            this.btn4.Text = "4";
            this.btn4.UseVisualStyleBackColor = false;
            this.btn4.Click += new System.EventHandler(this.btn4_Click);
            // 
            // btn5
            // 
            this.btn5.BackColor = System.Drawing.Color.Transparent;
            this.btn5.FlatAppearance.BorderSize = 3;
            this.btn5.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn5.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn5.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn5.ForeColor = System.Drawing.Color.White;
            this.btn5.Location = new System.Drawing.Point(144, 276);
            this.btn5.Name = "btn5";
            this.btn5.Size = new System.Drawing.Size(90, 47);
            this.btn5.TabIndex = 1;
            this.btn5.Text = "5";
            this.btn5.UseVisualStyleBackColor = false;
            this.btn5.Click += new System.EventHandler(this.btn5_Click);
            // 
            // btn6
            // 
            this.btn6.BackColor = System.Drawing.Color.Transparent;
            this.btn6.FlatAppearance.BorderSize = 3;
            this.btn6.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn6.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn6.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn6.ForeColor = System.Drawing.Color.White;
            this.btn6.Location = new System.Drawing.Point(260, 276);
            this.btn6.Name = "btn6";
            this.btn6.Size = new System.Drawing.Size(90, 47);
            this.btn6.TabIndex = 1;
            this.btn6.Text = "6";
            this.btn6.UseVisualStyleBackColor = false;
            this.btn6.Click += new System.EventHandler(this.btn6_Click);
            // 
            // btnSub
            // 
            this.btnSub.BackColor = System.Drawing.Color.Transparent;
            this.btnSub.FlatAppearance.BorderSize = 3;
            this.btnSub.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnSub.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnSub.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnSub.Font = new System.Drawing.Font("宋体", 20F, System.Drawing.FontStyle.Bold);
            this.btnSub.ForeColor = System.Drawing.Color.White;
            this.btnSub.Location = new System.Drawing.Point(376, 276);
            this.btnSub.Name = "btnSub";
            this.btnSub.Size = new System.Drawing.Size(90, 47);
            this.btnSub.TabIndex = 1;
            this.btnSub.Text = "-";
            this.btnSub.UseVisualStyleBackColor = false;
            this.btnSub.Click += new System.EventHandler(this.btnSub_Click);
            // 
            // btn7
            // 
            this.btn7.BackColor = System.Drawing.Color.Transparent;
            this.btn7.FlatAppearance.BorderSize = 3;
            this.btn7.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn7.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn7.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn7.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn7.ForeColor = System.Drawing.Color.White;
            this.btn7.Location = new System.Drawing.Point(28, 361);
            this.btn7.Name = "btn7";
            this.btn7.Size = new System.Drawing.Size(90, 47);
            this.btn7.TabIndex = 1;
            this.btn7.Text = "7";
            this.btn7.UseVisualStyleBackColor = false;
            this.btn7.Click += new System.EventHandler(this.btn7_Click);
            // 
            // btn8
            // 
            this.btn8.BackColor = System.Drawing.Color.Transparent;
            this.btn8.FlatAppearance.BorderSize = 3;
            this.btn8.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn8.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn8.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn8.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn8.ForeColor = System.Drawing.Color.White;
            this.btn8.Location = new System.Drawing.Point(144, 361);
            this.btn8.Name = "btn8";
            this.btn8.Size = new System.Drawing.Size(90, 47);
            this.btn8.TabIndex = 1;
            this.btn8.Text = "8";
            this.btn8.UseVisualStyleBackColor = false;
            this.btn8.Click += new System.EventHandler(this.btn8_Click);
            // 
            // btn9
            // 
            this.btn9.BackColor = System.Drawing.Color.Transparent;
            this.btn9.FlatAppearance.BorderSize = 3;
            this.btn9.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn9.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn9.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn9.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn9.ForeColor = System.Drawing.Color.White;
            this.btn9.Location = new System.Drawing.Point(260, 361);
            this.btn9.Name = "btn9";
            this.btn9.Size = new System.Drawing.Size(90, 47);
            this.btn9.TabIndex = 1;
            this.btn9.Text = "9";
            this.btn9.UseVisualStyleBackColor = false;
            this.btn9.Click += new System.EventHandler(this.btn9_Click);
            // 
            // btnMul
            // 
            this.btnMul.BackColor = System.Drawing.Color.Transparent;
            this.btnMul.FlatAppearance.BorderSize = 3;
            this.btnMul.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnMul.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnMul.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnMul.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnMul.ForeColor = System.Drawing.Color.White;
            this.btnMul.Location = new System.Drawing.Point(376, 361);
            this.btnMul.Name = "btnMul";
            this.btnMul.Size = new System.Drawing.Size(90, 47);
            this.btnMul.TabIndex = 1;
            this.btnMul.Text = "*";
            this.btnMul.UseVisualStyleBackColor = false;
            this.btnMul.Click += new System.EventHandler(this.btnMul_Click);
            // 
            // btnDot
            // 
            this.btnDot.BackColor = System.Drawing.Color.Transparent;
            this.btnDot.FlatAppearance.BorderSize = 3;
            this.btnDot.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnDot.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnDot.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnDot.Font = new System.Drawing.Font("宋体", 19.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnDot.ForeColor = System.Drawing.Color.White;
            this.btnDot.Location = new System.Drawing.Point(28, 443);
            this.btnDot.Name = "btnDot";
            this.btnDot.Size = new System.Drawing.Size(90, 47);
            this.btnDot.TabIndex = 1;
            this.btnDot.Text = ".";
            this.btnDot.UseVisualStyleBackColor = false;
            this.btnDot.Click += new System.EventHandler(this.btnDot_Click);
            // 
            // btn0
            // 
            this.btn0.BackColor = System.Drawing.Color.Transparent;
            this.btn0.FlatAppearance.BorderSize = 3;
            this.btn0.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btn0.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btn0.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn0.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btn0.ForeColor = System.Drawing.Color.White;
            this.btn0.Location = new System.Drawing.Point(144, 443);
            this.btn0.Name = "btn0";
            this.btn0.Size = new System.Drawing.Size(90, 47);
            this.btn0.TabIndex = 1;
            this.btn0.Text = "0";
            this.btn0.UseVisualStyleBackColor = false;
            this.btn0.Click += new System.EventHandler(this.btn0_Click);
            // 
            // btnDiv
            // 
            this.btnDiv.BackColor = System.Drawing.Color.Transparent;
            this.btnDiv.FlatAppearance.BorderSize = 3;
            this.btnDiv.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnDiv.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnDiv.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnDiv.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnDiv.ForeColor = System.Drawing.Color.White;
            this.btnDiv.Location = new System.Drawing.Point(260, 443);
            this.btnDiv.Name = "btnDiv";
            this.btnDiv.Size = new System.Drawing.Size(90, 47);
            this.btnDiv.TabIndex = 1;
            this.btnDiv.Text = "/";
            this.btnDiv.UseVisualStyleBackColor = false;
            this.btnDiv.Click += new System.EventHandler(this.btnDiv_Click);
            // 
            // btnEqual
            // 
            this.btnEqual.BackColor = System.Drawing.Color.Transparent;
            this.btnEqual.FlatAppearance.BorderSize = 3;
            this.btnEqual.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnEqual.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnEqual.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnEqual.Font = new System.Drawing.Font("宋体", 19.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnEqual.ForeColor = System.Drawing.Color.White;
            this.btnEqual.Location = new System.Drawing.Point(376, 520);
            this.btnEqual.Name = "btnEqual";
            this.btnEqual.Size = new System.Drawing.Size(207, 60);
            this.btnEqual.TabIndex = 1;
            this.btnEqual.Text = "=";
            this.btnEqual.UseVisualStyleBackColor = false;
            this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
            // 
            // btnAC
            // 
            this.btnAC.BackColor = System.Drawing.Color.Transparent;
            this.btnAC.FlatAppearance.BorderSize = 3;
            this.btnAC.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnAC.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnAC.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnAC.Font = new System.Drawing.Font("宋体", 19.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnAC.ForeColor = System.Drawing.Color.White;
            this.btnAC.Location = new System.Drawing.Point(28, 520);
            this.btnAC.Name = "btnAC";
            this.btnAC.Size = new System.Drawing.Size(206, 60);
            this.btnAC.TabIndex = 2;
            this.btnAC.Text = "AC";
            this.btnAC.UseVisualStyleBackColor = false;
            this.btnAC.Click += new System.EventHandler(this.btnAC_Click);
            // 
            // btncos
            // 
            this.btncos.BackColor = System.Drawing.Color.Transparent;
            this.btncos.FlatAppearance.BorderSize = 3;
            this.btncos.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btncos.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btncos.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btncos.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btncos.ForeColor = System.Drawing.Color.White;
            this.btncos.Location = new System.Drawing.Point(493, 443);
            this.btncos.Name = "btncos";
            this.btncos.Size = new System.Drawing.Size(90, 47);
            this.btncos.TabIndex = 3;
            this.btncos.Text = "cos";
            this.btncos.UseVisualStyleBackColor = false;
            this.btncos.Click += new System.EventHandler(this.btncos_Click);
            // 
            // btnsin
            // 
            this.btnsin.BackColor = System.Drawing.Color.Transparent;
            this.btnsin.FlatAppearance.BorderSize = 3;
            this.btnsin.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnsin.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnsin.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnsin.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnsin.ForeColor = System.Drawing.Color.White;
            this.btnsin.Location = new System.Drawing.Point(493, 361);
            this.btnsin.Name = "btnsin";
            this.btnsin.Size = new System.Drawing.Size(90, 47);
            this.btnsin.TabIndex = 4;
            this.btnsin.Text = "sin";
            this.btnsin.UseVisualStyleBackColor = false;
            this.btnsin.Click += new System.EventHandler(this.btnsin_Click);
            // 
            // btnlog
            // 
            this.btnlog.BackColor = System.Drawing.Color.Transparent;
            this.btnlog.FlatAppearance.BorderSize = 3;
            this.btnlog.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnlog.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnlog.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnlog.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnlog.ForeColor = System.Drawing.Color.White;
            this.btnlog.Location = new System.Drawing.Point(493, 276);
            this.btnlog.Name = "btnlog";
            this.btnlog.Size = new System.Drawing.Size(90, 47);
            this.btnlog.TabIndex = 5;
            this.btnlog.Text = "log";
            this.btnlog.UseVisualStyleBackColor = false;
            this.btnlog.Click += new System.EventHandler(this.btnlog_Click);
            // 
            // btnex
            // 
            this.btnex.BackColor = System.Drawing.Color.Transparent;
            this.btnex.FlatAppearance.BorderSize = 3;
            this.btnex.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btnex.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btnex.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnex.Font = new System.Drawing.Font("宋体", 19.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnex.ForeColor = System.Drawing.Color.White;
            this.btnex.Location = new System.Drawing.Point(493, 193);
            this.btnex.Name = "btnex";
            this.btnex.Size = new System.Drawing.Size(90, 47);
            this.btnex.TabIndex = 6;
            this.btnex.Text = "^";
            this.btnex.UseVisualStyleBackColor = false;
            this.btnex.Click += new System.EventHandler(this.btnex_Click);
            // 
            // btntan
            // 
            this.btntan.BackColor = System.Drawing.Color.Transparent;
            this.btntan.FlatAppearance.BorderSize = 3;
            this.btntan.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Black;
            this.btntan.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Black;
            this.btntan.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btntan.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btntan.ForeColor = System.Drawing.Color.White;
            this.btntan.Location = new System.Drawing.Point(376, 443);
            this.btntan.Name = "btntan";
            this.btntan.Size = new System.Drawing.Size(90, 47);
            this.btntan.TabIndex = 7;
            this.btntan.Text = "tan";
            this.btntan.UseVisualStyleBackColor = false;
            this.btntan.Click += new System.EventHandler(this.btntan_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.Control;
            this.BackgroundImage = global::calculator.Properties.Resources.linhua;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(606, 601);
            this.Controls.Add(this.btntan);
            this.Controls.Add(this.btncos);
            this.Controls.Add(this.btnsin);
            this.Controls.Add(this.btnlog);
            this.Controls.Add(this.btnex);
            this.Controls.Add(this.btnAC);
            this.Controls.Add(this.btnEqual);
            this.Controls.Add(this.btnMul);
            this.Controls.Add(this.btnSub);
            this.Controls.Add(this.btnAdd);
            this.Controls.Add(this.btnDiv);
            this.Controls.Add(this.btn9);
            this.Controls.Add(this.btn6);
            this.Controls.Add(this.btn3);
            this.Controls.Add(this.btn0);
            this.Controls.Add(this.btn8);
            this.Controls.Add(this.btn5);
            this.Controls.Add(this.btn2);
            this.Controls.Add(this.btnDot);
            this.Controls.Add(this.btn7);
            this.Controls.Add(this.btn4);
            this.Controls.Add(this.btn1);
            this.Controls.Add(this.lblShow);
            this.Name = "Form1";
            this.Text = "calculator";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label lblShow;
        private System.Windows.Forms.Button btn1;
        private System.Windows.Forms.Button btn2;
        private System.Windows.Forms.Button btn3;
        private System.Windows.Forms.Button btnAdd;
        private System.Windows.Forms.Button btn4;
        private System.Windows.Forms.Button btn5;
        private System.Windows.Forms.Button btn6;
        private System.Windows.Forms.Button btnSub;
        private System.Windows.Forms.Button btn7;
        private System.Windows.Forms.Button btn8;
        private System.Windows.Forms.Button btn9;
        private System.Windows.Forms.Button btnMul;
        private System.Windows.Forms.Button btnDot;
        private System.Windows.Forms.Button btn0;
        private System.Windows.Forms.Button btnDiv;
        private System.Windows.Forms.Button btnEqual;
        private System.Windows.Forms.Button btnAC;
        private System.Windows.Forms.Button btncos;
        private System.Windows.Forms.Button btnsin;
        private System.Windows.Forms.Button btnlog;
        private System.Windows.Forms.Button btnex;
        private System.Windows.Forms.Button btntan;
    }
}


3. Handle User Input:

  • Write event-handling code to respond to user interactions. I create event handlers for button clicks.
  • When a number button is clicked, update the input display.
  • Implement event handlers for arithmetic operation buttons to perform calculations.

4. Perform Calculations:

  • Implement the logic for performing calculations based on user input. I keep track of the current input, previous input, and the selected arithmetic operation.
  • Handle calculations like addition, subtraction, multiplication, and division.
  • Display the result of calculations in the Label.

Flow Chart

Update Label
Set operator
Store Operand
Calculate Result
Display Result
Clear
Start
Initialize Component
Define Operand Variables
Define Operator Symbol
Number 0-9
lblShow
Operators + - * / ^ log sin cos tan
operator
op1
op1,op2,operator
Reset

5. Clear and Reset Functionality:

I provide a button AC to clear the input, reset the calculator.

6. Error Handling:

Implement error handling to handle scenarios such as division by zero or other invalid input. Display appropriate error messages to the user if necessary.

在这里插入图片描述

7. Testing and Debugging:

  • I test my calculator thoroughly to ensure that it performs accurate calculations and handles user input correctly.
  • Use debugging tools to identify and fix any issues in the code.

The code of Form.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //定义操作数
        float op1 = 0;
        float op2 = 0;
        double result = 0;

        //定义运算符号
        string symbol = null;

        //数字0-9
        private void btn1_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "1";
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "2";
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "3";
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "4";
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "5";
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "6";
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "7";
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "8";
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "9";
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "0" || lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "";
            }
            lblShow.Text += "0";
        }

        //运算符 + - * /
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "")
            {
                MessageBox.Show("输入有误", "计算错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "+";
                symbol = "+";
            }
            else{
                op1 = float.Parse(lblShow.Text);
                lblShow.Text = "+";
                symbol = "+";
            }
            

        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "")
            {
                MessageBox.Show("输入有误", "计算错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "-";
                symbol = "-";
            }
            else{
                op1 = float.Parse(lblShow.Text);
                lblShow.Text = "-";
                symbol = "-";
            }
        }

        private void btnMul_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "")
            {
                MessageBox.Show("输入有误", "计算错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "*";
                symbol = "*";
            }
            else{
                op1 = float.Parse(lblShow.Text);
                lblShow.Text = "*";
                symbol = "*";
            }
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "")
            {
                MessageBox.Show("输入有误", "计算错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "/";
                symbol = "/";
            }
            else{
                op1 = float.Parse(lblShow.Text);
                lblShow.Text = "/";
                symbol = "/";
            }
            
        }

        //小数点
        private void btnDot_Click(object sender, EventArgs e)
        {
            lblShow.Text += ".";
        }

        //=
        private void btnEqual_Click(object sender, EventArgs e)
        {
            op2 = float.Parse(lblShow.Text);
            switch(symbol){
                case "+":
                    result = op1 + op2;
                    symbol = "=";
                    break;
                case "-":
                    result = op1 - op2;
                    symbol = "=";
                    break;
                case "*":
                    result = op1 * op2;
                    symbol = "=";
                    break;
                case "/":
                    if (op2 == 0){
                        MessageBox.Show("除数不能为0!","计算错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        op2 = 1.0f;
                    }
                    result = op1 / op2;
                    symbol = "=";
                    break;
                case "^":
                    result = Math.Pow(op1,op2);;
                    symbol = "=";
                    break;
                case "log":
                    result = Math.Log10(op2); ;
                    symbol = "=";
                    break;
                case "sin":
                    result = Math.Sin(op2); ;
                    symbol = "=";
                    break;
                case "cos":
                    result = Math.Cos(op2); ;
                    symbol = "=";
                    break;
                case "tan":
                    result = Math.Tan(op2); ;
                    symbol = "=";
                    break;
                case "=":
                    symbol = "=";
                    break;
            }
            lblShow.Text = result.ToString();
          
        }

        //clear
        private void btnAC_Click(object sender, EventArgs e)
        {
            op1 = 0;
            op2 = 0;
            result = 0;
            lblShow.Text = "0";
        }

        //^ log sin cos tan
        private void btnex_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "")
            {
                MessageBox.Show("输入有误", "计算错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" ||lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "^";
                symbol = "^";
            }
            else
            {
                op1 = float.Parse(lblShow.Text);
                lblShow.Text = "^";
                symbol = "^";
            }
        }

        private void btnlog_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "log";
                symbol = "log";
            }
            else
            {
         		lblShow.Text = "log";
                symbol = "log";
            }
        }

        private void btnsin_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "sin";
                symbol = "sin";
            }
            else
            {
                lblShow.Text = "sin";
                symbol = "sin";
            }
        }

        private void btncos_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "cos";
                symbol = "cos";
            }
            else
            {
                lblShow.Text = "cos";
                symbol = "cos";
            }
        }

        private void btntan_Click(object sender, EventArgs e)
        {
            if (lblShow.Text == "+" || lblShow.Text == "-" || lblShow.Text == "*" || lblShow.Text == "/" || lblShow.Text == "^" || lblShow.Text == "log" || lblShow.Text == "sin" || lblShow.Text == "cos" || lblShow.Text == "tan")
            {
                lblShow.Text = "tan";
                symbol = "tan";
            }
            else
            {
                lblShow.Text = "tan";
                symbol = "tan";
            }
        }
    }
}

8. Deployment:

  • Finally, I compile my calculator application into an executable (.exe) file.
  • Distribute or deploy the calculator to users, either as a standalone application or as part of a larger software package.

V. Final Result Show

在这里插入图片描述

VI. Limitations and Future Plans

Limitations

  1. Can Only Handle Floating-Point Numbers: I’ve noticed that my calculator can only handle floating-point numbers and cannot handle integers. If users input integers, it automatically converts them to floating-point numbers, which may not meet user expectations.
  2. Input Restrictions: Although I’ve added some input restrictions in button click events, users can still input invalid characters, such as multiple operators or other non-numeric characters. I may need a more robust input validation mechanism to ensure input correctness.
  3. Lack of Bracket Support: Many calculators support brackets, allowing users to perform complex mathematical expression calculations. My calculator seems to lack support for brackets, limiting its functionality.
  4. Error Handling: While I’ve added error message boxes in some cases to handle incorrect inputs, this isn’t the best user experience. A better approach would be to provide real-time feedback on the interface, such as clearing the current input and displaying error messages for incorrect input.
  5. No Support for Clearing Individual Characters: Users might want the ability to delete individual characters from their input, but my calculator currently doesn’t offer this feature.

Future Plans

  1. Code Reusability: I will consolidate event handlers for the number buttons to reduce redundant code. This will enhance code maintainability.
  2. Function Encapsulation: I will create functions to perform common operations, such as checking the validity of input. This will improve code readability.
  3. UI Enhancements: I will add some user-friendly features such as backspace, clear all, undo, etc., to enhance the user experience.
  4. Unit Testing: I will add unit tests for the calculation logic to ensure it works correctly in different scenarios.
  5. Layered Design: If I plan to extend the application into a more complex one, I will use a layered design to isolate the interface, calculation logic, and data layers.

VII. Summary

When I created a calculator project using WinForms and wrote my first Markdown blog post to document this project, I gained a lot of insights and takeaways.

Firstly, I learned how to use WinForms to create a simple desktop application. WinForms is a powerful tool that allows developers to easily build Windows applications. By learning how to design the interface, add controls, handle user input, and grasp basic WinForms concepts, I was able to create a basic calculator application. Throughout this process, I understood how WinForms operates, including event handling, creating user interfaces, and implementing application logic.

Secondly, I experienced the convenience of Markdown syntax. Markdown is a lightweight markup language used for writing documents and blogs. Its syntax is simple and easy to learn, enabling the quick generation of well-formatted documents. While writing the blog, I used Markdown markup to create headings, paragraphs, lists, links, and other elements without the need to delve into complex HTML or other markup languages. This allowed me to focus on expressing my thoughts and experiences without worrying about document formatting.

Furthermore, by documenting my project experience, I improved my communication and sharing skills. Writing a blog is an excellent way to share my thoughts and experiences with others. By articulating my ideas clearly, I can help others understand how to create a simple WinForms application and share my learning process and discoveries. It also helps me better understand and summarize my work, enhancing my programming skills.

Lastly, this project and blog reinforced my interest in computer programming. Through practical projects, I was able to apply theoretical knowledge to practice, which improved my understanding of the essence of programming. Additionally, sharing my project experience allows me to learn from and exchange ideas with other programming enthusiasts, further igniting my passion.

In conclusion, by creating a calculator project using WinForms and writing a Markdown blog, I learned important skills related to application development, document writing, and knowledge sharing. This was not only a fun learning experience but also an opportunity for personal growth, and I look forward to continuing to explore and develop these skills in the future.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值