C# - Simple Calculator
Submitted by razormist on Monday, March 26, 2018 - 23:54.
In this tutorial we will create a Simple Calculator using C#. C# is a general-purpose, object-oriented programming language. C# is expected to make it faster and less expensive to develop new application. The potential of C# is great when it comes in developing desktop application. It has a friendly environment for all new developers. So let's do the coding.
or also you create the layout by dragging the designating tools to the forms.
Try to run the application and see if it works.
There you go we successfully create a Simple Calculator using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Getting Started
First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/Application Design
We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Then write these codes inside your designer file.- namespace Simple_Calculator
- {
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.SuspendLayout();
- //
- // btn_one
- //
- this.btn_one.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_one.Name = "btn_one";
- this.btn_one.TabIndex = 0;
- this.btn_one.Text = "1";
- this.btn_one.UseVisualStyleBackColor = true;
- //
- // btn_two
- //
- this.btn_two.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_two.Name = "btn_two";
- this.btn_two.TabIndex = 1;
- this.btn_two.Text = "2";
- this.btn_two.UseVisualStyleBackColor = true;
- //
- // btn_three
- //
- this.btn_three.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_three.Name = "btn_three";
- this.btn_three.TabIndex = 2;
- this.btn_three.Text = "3";
- this.btn_three.UseVisualStyleBackColor = true;
- //
- // btn_subtract
- //
- this.btn_subtract.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_subtract.Name = "btn_subtract";
- this.btn_subtract.TabIndex = 3;
- this.btn_subtract.Text = "-";
- this.btn_subtract.UseVisualStyleBackColor = true;
- //
- // btn_four
- //
- this.btn_four.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_four.Name = "btn_four";
- this.btn_four.TabIndex = 4;
- this.btn_four.Text = "4";
- this.btn_four.UseVisualStyleBackColor = true;
- //
- // btn_five
- //
- this.btn_five.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_five.Name = "btn_five";
- this.btn_five.TabIndex = 5;
- this.btn_five.Text = "5";
- this.btn_five.UseVisualStyleBackColor = true;
- //
- // btn_six
- //
- this.btn_six.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_six.Name = "btn_six";
- this.btn_six.TabIndex = 6;
- this.btn_six.Text = "6";
- this.btn_six.UseVisualStyleBackColor = true;
- //
- // btn_multiply
- //
- this.btn_multiply.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_multiply.Name = "btn_multiply";
- this.btn_multiply.TabIndex = 7;
- this.btn_multiply.Text = "*";
- this.btn_multiply.UseVisualStyleBackColor = true;
- //
- // btn_seven
- //
- this.btn_seven.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_seven.Name = "btn_seven";
- this.btn_seven.TabIndex = 8;
- this.btn_seven.Text = "7";
- this.btn_seven.UseVisualStyleBackColor = true;
- //
- // btn_eight
- //
- this.btn_eight.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_eight.Name = "btn_eight";
- this.btn_eight.TabIndex = 9;
- this.btn_eight.Text = "8";
- this.btn_eight.UseVisualStyleBackColor = true;
- //
- // btn_nine
- //
- this.btn_nine.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_nine.Name = "btn_nine";
- this.btn_nine.TabIndex = 10;
- this.btn_nine.Text = "9";
- this.btn_nine.UseVisualStyleBackColor = true;
- //
- // btn_divide
- //
- this.btn_divide.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_divide.Name = "btn_divide";
- this.btn_divide.TabIndex = 11;
- this.btn_divide.Text = "/";
- this.btn_divide.UseVisualStyleBackColor = true;
- //
- // btn_addition
- //
- this.btn_addition.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_addition.Name = "btn_addition";
- this.btn_addition.TabIndex = 15;
- this.btn_addition.Text = "+";
- this.btn_addition.UseVisualStyleBackColor = true;
- //
- // btn_dot
- //
- this.btn_dot.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_dot.Name = "btn_dot";
- this.btn_dot.TabIndex = 14;
- this.btn_dot.Text = ".";
- this.btn_dot.UseVisualStyleBackColor = true;
- //
- // btn_zero
- //
- this.btn_zero.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_zero.Name = "btn_zero";
- this.btn_zero.TabIndex = 13;
- this.btn_zero.Text = "0";
- this.btn_zero.UseVisualStyleBackColor = true;
- //
- // btn_equal
- //
- this.btn_equal.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_equal.Name = "btn_equal";
- this.btn_equal.TabIndex = 12;
- this.btn_equal.Text = "=";
- this.btn_equal.UseVisualStyleBackColor = true;
- //
- // btn_clearall
- //
- this.btn_clearall.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_clearall.Name = "btn_clearall";
- this.btn_clearall.TabIndex = 16;
- this.btn_clearall.Text = "CE";
- this.btn_clearall.UseVisualStyleBackColor = true;
- //
- // btn_clear
- //
- this.btn_clear.Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_clear.Name = "btn_clear";
- this.btn_clear.TabIndex = 17;
- this.btn_clear.Text = "C";
- this.btn_clear.UseVisualStyleBackColor = true;
- //
- // tbox_result
- //
- this.tbox_result.Font = new System.Drawing.Font("Arial", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.tbox_result.Multiline = true;
- this.tbox_result.Name = "tbox_result";
- this.tbox_result.TabIndex = 18;
- this.tbox_result.Text = "0";
- this.tbox_result.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
- //
- // lbl_display
- //
- this.lbl_display.AutoSize = true;
- this.lbl_display.BackColor = System.Drawing.Color.White;
- this.lbl_display.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.lbl_display.ForeColor = System.Drawing.SystemColors.ActiveBorder;
- this.lbl_display.Name = "lbl_display";
- this.lbl_display.TabIndex = 19;
- //
- // Form1
- //
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.BackColor = System.Drawing.Color.SkyBlue;
- this.Controls.Add(this.lbl_display);
- this.Controls.Add(this.tbox_result);
- this.Controls.Add(this.btn_clear);
- this.Controls.Add(this.btn_clearall);
- this.Controls.Add(this.btn_addition);
- this.Controls.Add(this.btn_dot);
- this.Controls.Add(this.btn_zero);
- this.Controls.Add(this.btn_equal);
- this.Controls.Add(this.btn_divide);
- this.Controls.Add(this.btn_nine);
- this.Controls.Add(this.btn_eight);
- this.Controls.Add(this.btn_seven);
- this.Controls.Add(this.btn_multiply);
- this.Controls.Add(this.btn_six);
- this.Controls.Add(this.btn_five);
- this.Controls.Add(this.btn_four);
- this.Controls.Add(this.btn_subtract);
- this.Controls.Add(this.btn_three);
- this.Controls.Add(this.btn_two);
- this.Controls.Add(this.btn_one);
- this.Name = "Form1";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Simple Calculator";
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.Button btn_one;
- private System.Windows.Forms.Button btn_two;
- private System.Windows.Forms.Button btn_three;
- private System.Windows.Forms.Button btn_subtract;
- private System.Windows.Forms.Button btn_four;
- private System.Windows.Forms.Button btn_five;
- private System.Windows.Forms.Button btn_six;
- private System.Windows.Forms.Button btn_multiply;
- private System.Windows.Forms.Button btn_seven;
- private System.Windows.Forms.Button btn_eight;
- private System.Windows.Forms.Button btn_nine;
- private System.Windows.Forms.Button btn_divide;
- private System.Windows.Forms.Button btn_addition;
- private System.Windows.Forms.Button btn_dot;
- private System.Windows.Forms.Button btn_zero;
- private System.Windows.Forms.Button btn_equal;
- private System.Windows.Forms.Button btn_clearall;
- private System.Windows.Forms.Button btn_clear;
- private System.Windows.Forms.TextBox tbox_result;
- private System.Windows.Forms.Label lbl_display;
- }
- }
Creating the C# script
This code contains the function of the application. This code will will display some number and equation every time the buttons is clicked. To do that locate the csharp script, In my case the default script will be called Form1.cs. Then write these block of codes inside the Class of the form called Form1.- Double value = 0;
- String operation = "";
- bool operation_pressed = false;
- public Form1()
- {
- InitializeComponent();
- }
- private void btn_one_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed) {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "1";
- }
- private void btn_two_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "2";
- }
- private void btn_three_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "3";
- }
- private void btn_four_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "4";
- }
- private void btn_five_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "5";
- }
- private void btn_six_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "6";
- }
- private void btn_seven_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "7";
- }
- private void btn_eight_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "8";
- }
- private void btn_nine_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "9";
- }
- private void btn_zero_Click(object sender, EventArgs e)
- {
- if (tbox_result.Text == "0" || operation_pressed)
- {
- tbox_result.Clear();
- }
- operation_pressed = false;
- tbox_result.Text += "0";
- }
- private void btn_clearall_Click(object sender, EventArgs e)
- {
- tbox_result.Text = "0";
- lbl_display.Text = "";
- }
- private void btn_addition_Click(object sender, EventArgs e)
- {
- operation = "+";
- value = Double.Parse(tbox_result.Text);
- operation_pressed = true;
- lbl_display.Text = value + " " + operation;
- }
- private void btn_subtract_Click(object sender, EventArgs e)
- {
- operation = "-";
- value = Double.Parse(tbox_result.Text);
- operation_pressed = true;
- lbl_display.Text = value + " " + operation;
- }
- private void btn_multiply_Click(object sender, EventArgs e)
- {
- operation = "*";
- value = Double.Parse(tbox_result.Text);
- operation_pressed = true;
- lbl_display.Text = value + " " + operation;
- }
- private void btn_divide_Click(object sender, EventArgs e)
- {
- operation = "/";
- value = Double.Parse(tbox_result.Text);
- operation_pressed = true;
- lbl_display.Text = value + " " + operation;
- }
- private void btn_equal_Click(object sender, EventArgs e)
- {
- lbl_display.Text = "";
- if (operation != "") {
- switch (operation)
- {
- case "+":
- tbox_result.Text = (value + Double.Parse(tbox_result.Text)).ToString();
- break;
- case "-":
- tbox_result.Text = (value - Double.Parse(tbox_result.Text)).ToString();
- break;
- case "*":
- tbox_result.Text = (value * Double.Parse(tbox_result.Text)).ToString();
- break;
- case "/":
- tbox_result.Text = (value / Double.Parse(tbox_result.Text)).ToString();
- break;
- default:
- break;
- }
- }
- operation = "";
- }
- private void btn_clear_Click(object sender, EventArgs e)
- {
- if (tbox_result.TextLength > 1)
- {
- tbox_result.Text = tbox_result.Text.Substring(0, tbox_result.TextLength - 1);
- }
- else {
- tbox_result.Text = "0";
- }
- }
Add new comment
- 1963 views