C# Calculator App Using WinForms
Explore the functionality of our calculator application built with C# and Windows Forms. Copy this code!
MainForm class (MainForm.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 CalculatorApp { public partial class MainForm : Form { string operationSymbol; double number; bool buttonClicked = false; public MainForm() { InitializeComponent(); } private void btn0_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "0"; } else { textBox1.Text = textBox1.Text + "0"; } buttonClicked = true; } private void btn1_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "1"; } else { textBox1.Text = textBox1.Text + "1"; } buttonClicked = true; } private void btn2_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "2"; } else { textBox1.Text = textBox1.Text + "2"; } buttonClicked = true; } private void btn3_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "3"; } else { textBox1.Text = textBox1.Text + "3"; } buttonClicked = true; } private void btn4_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "4"; } else { textBox1.Text = textBox1.Text + "4"; } buttonClicked = true; } private void btn5_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "5"; } else { textBox1.Text = textBox1.Text + "5"; } buttonClicked = true; } private void btn6_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "6"; } else { textBox1.Text = textBox1.Text + "6"; } buttonClicked = true; } private void btn7_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "7"; } else { textBox1.Text = textBox1.Text + "7"; } buttonClicked = true; } private void btn8_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "8"; } else { textBox1.Text = textBox1.Text + "8"; } buttonClicked = true; } private void btn9_Click(object sender, EventArgs e) { if (textBox1.Text != null && textBox1.Text == "0") { textBox1.Text = "9"; } else { textBox1.Text = textBox1.Text + "9"; } buttonClicked = true; } private void btnPlus_Click(object sender, EventArgs e) { if (buttonClicked == true && textBox1.Text != "") { number = Convert.ToDouble(textBox1.Text); textBox1.Text = "0"; operationSymbol = "+"; } buttonClicked = false; } private void btnMinus_Click(object sender, EventArgs e) { if (buttonClicked == true && textBox1.Text != "") { number = Convert.ToDouble(textBox1.Text); textBox1.Text = "0"; operationSymbol = "-"; } buttonClicked = false; } private void btnMultiply_Click(object sender, EventArgs e) { if (buttonClicked == true && textBox1.Text != "") { number = Convert.ToDouble(textBox1.Text); textBox1.Text = "0"; operationSymbol = "*"; } buttonClicked = false; } private void btnDivide_Click(object sender, EventArgs e) { if (buttonClicked == true && textBox1.Text != "") { number = Convert.ToDouble(textBox1.Text); textBox1.Text = "0"; operationSymbol = "/"; } buttonClicked = false; } private void btnClear_Click(object sender, EventArgs e) { textBox1.Text = ""; } private void btnEquals_Click(object sender, EventArgs e) { double secondNumber; double result; if (textBox1.Text != "") { secondNumber = Convert.ToDouble(textBox1.Text); if (operationSymbol == "+") { result = (number + secondNumber); textBox1.Text = Convert.ToString(result); } if (operationSymbol == "-") { result = (number - secondNumber); textBox1.Text = Convert.ToString(result); } if (operationSymbol == "*") { result = (number * secondNumber); textBox1.Text = Convert.ToString(result); } if (operationSymbol == "/") { if (secondNumber == 0) { textBox1.Text = "Error"; } else { result = (number / secondNumber); textBox1.Text = Convert.ToString(result); number = result; } } } } } }
Program class (Program.cs):
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace CalculatorApp { internal static class Program { ////// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } }
Partial class MainForm (MainForm.Designer.cs):
namespace CalculatorApp { partial class MainForm { private System.ComponentModel.IContainer components = null; /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code ////// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btn1 = new System.Windows.Forms.Button(); this.btn2 = new System.Windows.Forms.Button(); this.btn5 = new System.Windows.Forms.Button(); this.btn4 = new System.Windows.Forms.Button(); this.btn3 = new System.Windows.Forms.Button(); this.btn6 = 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.btn0 = new System.Windows.Forms.Button(); this.btnAdd = new System.Windows.Forms.Button(); this.btnSubtract = new System.Windows.Forms.Button(); this.btnMultiply = new System.Windows.Forms.Button(); this.btnDivide = new System.Windows.Forms.Button(); this.btnEquals = new System.Windows.Forms.Button(); this.btnClear = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // btn1 // this.btn1.Location = new System.Drawing.Point(48, 139); this.btn1.Name = "btn1"; this.btn1.Size = new System.Drawing.Size(56, 52); this.btn1.TabIndex = 0; this.btn1.Text = "1"; this.btn1.UseVisualStyleBackColor = true; this.btn1.Click += new System.EventHandler(this.btn1_Click); // // btn2 // this.btn2.Location = new System.Drawing.Point(110, 139); this.btn2.Name = "btn2"; this.btn2.Size = new System.Drawing.Size(56, 52); this.btn2.TabIndex = 1; this.btn2.Text = "2"; this.btn2.UseVisualStyleBackColor = true; this.btn2.Click += new System.EventHandler(this.btn2_Click); // // btn5 // this.btn5.Location = new System.Drawing.Point(110, 197); this.btn5.Name = "btn5"; this.btn5.Size = new System.Drawing.Size(56, 52); this.btn5.TabIndex = 2; this.btn5.Text = "5"; this.btn5.UseVisualStyleBackColor = true; this.btn5.Click += new System.EventHandler(this.btn5_Click); // // btn4 // this.btn4.Location = new System.Drawing.Point(48, 197); this.btn4.Name = "btn4"; this.btn4.Size = new System.Drawing.Size(56, 52); this.btn4.TabIndex = 3; this.btn4.Text = "4"; this.btn4.UseVisualStyleBackColor = true; this.btn4.Click += new System.EventHandler(this.btn4_Click); // // btn3 // this.btn3.Location = new System.Drawing.Point(172, 139); this.btn3.Name = "btn3"; this.btn3.Size = new System.Drawing.Size(56, 52); this.btn3.TabIndex = 3; this.btn3.Text = "3"; this.btn3.UseVisualStyleBackColor = true; this.btn3.Click += new System.EventHandler(this.btn3_Click); // // btn6 // this.btn6.Location = new System.Drawing.Point(172, 197); this.btn6.Name = "btn6"; this.btn6.Size = new System.Drawing.Size(56, 52); this.btn6.TabIndex = 4; this.btn6.Text = "6"; this.btn6.UseVisualStyleBackColor = true; this.btn6.Click += new System.EventHandler(this.btn6_Click); // // btn7 // this.btn7.Location = new System.Drawing.Point(48, 255); this.btn7.Name = "btn7"; this.btn7.Size = new System.Drawing.Size(56, 52); this.btn7.TabIndex = 5; this.btn7.Text = "7"; this.btn7.UseVisualStyleBackColor = true; this.btn7.Click += new System.EventHandler(this.btn7_Click); // // btn8 // this.btn8.Location = new System.Drawing.Point(110, 255); this.btn8.Name = "btn8"; this.btn8.Size = new System.Drawing.Size(56, 52); this.btn8.TabIndex = 6; this.btn8.Text = "8"; this.btn8.UseVisualStyleBackColor = true; this.btn8.Click += new System.EventHandler(this.btn8_Click); // // btn9 // this.btn9.Location = new System.Drawing.Point(172, 255); this.btn9.Name = "btn9"; this.btn9.Size = new System.Drawing.Size(56, 52); this.btn9.TabIndex = 7; this.btn9.Text = "9"; this.btn9.UseVisualStyleBackColor = true; this.btn9.Click += new System.EventHandler(this.btn9_Click); // // btn0 // this.btn0.Location = new System.Drawing.Point(110, 313); this.btn0.Name = "btn0"; this.btn0.Size = new System.Drawing.Size(56, 52); this.btn0.TabIndex = 8; this.btn0.Text = "0"; this.btn0.UseVisualStyleBackColor = true; this.btn0.Click += new System.EventHandler(this.btn0_Click); // // btnAdd // this.btnAdd.Location = new System.Drawing.Point(234, 139); this.btnAdd.Name = "btnAdd"; this.btnAdd.Size = new System.Drawing.Size(56, 52); this.btnAdd.TabIndex = 9; this.btnAdd.Text = "+"; this.btnAdd.UseVisualStyleBackColor = true; this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click); // // btnSubtract // this.btnSubtract.Location = new System.Drawing.Point(234, 197); this.btnSubtract.Name = "btnSubtract"; this.btnSubtract.Size = new System.Drawing.Size(56, 52); this.btnSubtract.TabIndex = 10; this.btnSubtract.Text = "-"; this.btnSubtract.UseVisualStyleBackColor = true; this.btnSubtract.Click += new System.EventHandler(this.btnSubtract_Click); // // btnMultiply // this.btnMultiply.Location = new System.Drawing.Point(234, 255); this.btnMultiply.Name = "btnMultiply"; this.btnMultiply.Size = new System.Drawing.Size(56, 52); this.btnMultiply.TabIndex = 11; this.btnMultiply.Text = "x"; this.btnMultiply.UseVisualStyleBackColor = true; this.btnMultiply.Click += new System.EventHandler(this.btnMultiply_Click); // // btnDivide // this.btnDivide.Location = new System.Drawing.Point(234, 313); this.btnDivide.Name = "btnDivide"; this.btnDivide.Size = new System.Drawing.Size(56, 52); this.btnDivide.TabIndex = 12; this.btnDivide.Text = "/"; this.btnDivide.UseVisualStyleBackColor = true; this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click); // // btnEquals // this.btnEquals.Location = new System.Drawing.Point(172, 313); this.btnEquals.Name = "btnEquals"; this.btnEquals.Size = new System.Drawing.Size(56, 52); this.btnEquals.TabIndex = 13; this.btnEquals.Text = "="; this.btnEquals.UseVisualStyleBackColor = true; this.btnEquals.Click += new System.EventHandler(this.btnEquals_Click); // // btnClear // this.btnClear.Location = new System.Drawing.Point(48, 313); this.btnClear.Name = "btnClear"; this.btnClear.Size = new System.Drawing.Size(56, 52); this.btnClear.TabIndex = 14; this.btnClear.Text = "C"; this.btnClear.UseVisualStyleBackColor = true; this.btnClear.Click += new System.EventHandler(this.btnClear_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(48, 107); this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(242, 26); this.textBox1.TabIndex = 15; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(468, 450); this.Controls.Add(this.textBox1); this.Controls.Add(this.btnClear); this.Controls.Add(this.btnEquals); this.Controls.Add(this.btnDivide); this.Controls.Add(this.btnMultiply); this.Controls.Add(this.btnSubtract); this.Controls.Add(this.btnAdd); this.Controls.Add(this.btn0); this.Controls.Add(this.btn9); this.Controls.Add(this.btn8); this.Controls.Add(this.btn7); this.Controls.Add(this.btn6); this.Controls.Add(this.btn3); this.Controls.Add(this.btn4); this.Controls.Add(this.btn5); this.Controls.Add(this.btn2); this.Controls.Add(this.btn1); this.Name = "MainForm"; this.Text = "C# Calculator App"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btn1; private System.Windows.Forms.Button btn2; private System.Windows.Forms.Button btn5; private System.Windows.Forms.Button btn4; private System.Windows.Forms.Button btn3; private System.Windows.Forms.Button btn6; private System.Windows.Forms.Button btn7; private System.Windows.Forms.Button btn8; private System.Windows.Forms.Button btn9; private System.Windows.Forms.Button btn0; private System.Windows.Forms.Button btnAdd; private System.Windows.Forms.Button btnSubtract; private System.Windows.Forms.Button btnMultiply; private System.Windows.Forms.Button btnDivide; private System.Windows.Forms.Button btnEquals; private System.Windows.Forms.Button btnClear; private System.Windows.Forms.TextBox textBox1; } }