In this blog we will learn how to create a basic calculator using c#. This is very basic calculator for beginners, who want to learn basic concepts of conditions (If, Else , Else If) using c#. In this article, I will make a calculator without using code optimization.
Code For UI
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button17 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
// button1
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(33, 142);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 41);
this.button1.TabIndex = 0;
this.button1.Text = "1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
this.textBox1.Location = new System.Drawing.Point(24, 34);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(475, 55);
this.textBox1.TabIndex = 1;
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button18);
this.groupBox1.Controls.Add(this.button17);
this.groupBox1.Controls.Add(this.button16);
this.groupBox1.Controls.Add(this.button15);
this.groupBox1.Controls.Add(this.button14);
this.groupBox1.Controls.Add(this.button13);
this.groupBox1.Controls.Add(this.button12);
this.groupBox1.Controls.Add(this.button11);
this.groupBox1.Controls.Add(this.button10);
this.groupBox1.Controls.Add(this.button9);
this.groupBox1.Controls.Add(this.button8);
this.groupBox1.Controls.Add(this.button7);
this.groupBox1.Controls.Add(this.button6);
this.groupBox1.Controls.Add(this.button5);
this.groupBox1.Controls.Add(this.button4);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(12, 25);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(530, 328);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "CalC";
//
// button17
//
this.button17.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button17.Location = new System.Drawing.Point(387, 95);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(112, 41);
this.button17.TabIndex = 17;
this.button17.Text = "BackSpace";
this.button17.UseVisualStyleBackColor = true;
this.button17.Click += new System.EventHandler(this.button17_Click);
//
// button16
//
this.button16.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button16.Location = new System.Drawing.Point(387, 142);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(112, 41);
this.button16.TabIndex = 16;
this.button16.Text = "/";
this.button16.UseVisualStyleBackColor = true;
this.button16.Click += new System.EventHandler(this.button16_Click);
//
// button15
//
this.button15.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button15.Location = new System.Drawing.Point(387, 186);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(112, 41);
this.button15.TabIndex = 15;
this.button15.Text = "X";
this.button15.UseVisualStyleBackColor = true;
this.button15.Click += new System.EventHandler(this.button15_Click);
//
// button14
//
this.button14.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button14.Location = new System.Drawing.Point(387, 229);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(112, 41);
this.button14.TabIndex = 14;
this.button14.Text = "-";
this.button14.UseVisualStyleBackColor = true;
this.button14.Click += new System.EventHandler(this.button14_Click);
//
// button13
//
this.button13.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button13.Location = new System.Drawing.Point(387, 273);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(112, 41);
this.button13.TabIndex = 13;
this.button13.Text = "+";
this.button13.UseVisualStyleBackColor = true;
this.button13.Click += new System.EventHandler(this.button13_Click);
//
// button12
//
this.button12.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button12.Location = new System.Drawing.Point(269, 273);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(112, 41);
this.button12.TabIndex = 12;
this.button12.Text = "=";
this.button12.UseVisualStyleBackColor = true;
this.button12.Click += new System.EventHandler(this.button12_Click);
//
// button11
//
this.button11.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button11.Location = new System.Drawing.Point(151, 273);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(112, 41);
this.button11.TabIndex = 11;
this.button11.Text = ".";
this.button11.UseVisualStyleBackColor = true;
this.button11.Click += new System.EventHandler(this.button11_Click);
//
// button10
//
this.button10.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button10.Location = new System.Drawing.Point(33, 273);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(112, 41);
this.button10.TabIndex = 10;
this.button10.Text = "0";
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// button9
//
this.button9.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button9.Location = new System.Drawing.Point(269, 229);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(112, 41);
this.button9.TabIndex = 9;
this.button9.Text = "9";
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button8
//
this.button8.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button8.Location = new System.Drawing.Point(151, 229);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(112, 41);
this.button8.TabIndex = 8;
this.button8.Text = "8";
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button7
//
this.button7.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button7.Location = new System.Drawing.Point(33, 229);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(112, 41);
this.button7.TabIndex = 7;
this.button7.Text = "7";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button6
//
this.button6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button6.Location = new System.Drawing.Point(269, 186);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(112, 41);
this.button6.TabIndex = 6;
this.button6.Text = "6";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button5
//
this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button5.Location = new System.Drawing.Point(151, 186);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(112, 41);
this.button5.TabIndex = 5;
this.button5.Text = "5";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button4
//
this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button4.Location = new System.Drawing.Point(33, 186);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(112, 41);
this.button4.TabIndex = 4;
this.button4.Text = "4";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button3.Location = new System.Drawing.Point(269, 142);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(112, 41);
this.button3.TabIndex = 3;
this.button3.Text = "3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(151, 142);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(112, 41);
this.button2.TabIndex = 2;
this.button2.Text = "2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button18
//
this.button18.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button18.Location = new System.Drawing.Point(269, 95);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(112, 41);
this.button18.TabIndex = 18;
this.button18.Text = "Clear";
this.button18.UseVisualStyleBackColor = true;
this.button18.Click += new System.EventHandler(this.button18_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(574, 380);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
}
Business Logic of the calculator:
Business Logic of the calculator:
Ones you create UI ones then write the code for functionality. I mentioned there everything in step by step. You have also follow these steps.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double val1, val2,result;
int eq=0, v=1,op,count;
String str = null;
//Code for numbers
private void button1_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button1.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button1.Text;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button2.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button2.Text;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button3.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button3.Text;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button4.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button4.Text;
}
}
private void button5_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button5.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button5.Text;
}
}
private void button6_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button6.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button6.Text;
}
}
private void button7_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button7.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button7.Text;
}
}
private void button8_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button8.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button8.Text;
}
}
private void button9_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button9.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button9.Text;
}
}
private void button10_Click(object sender, EventArgs e)
{
if (v == 0)
{
textBox1.Text = button10.Text;
v = 1;
}
else
{
textBox1.Text = textBox1.Text + button10.Text;
}
}
//__________________ends numbers__________________________
//_________________For Decimal _____________
private void button11_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Contains("."))
{
textBox1.Text =textBox1.Text + button11.Text;
}
}
//______________End Decimal______________________
//______________For Equal________________________
private void button12_Click(object sender, EventArgs e)
{
if (eq == 1)
{
val2 = double.Parse(textBox1.Text);
eq = 0;
}
else
{
val1= double.Parse(textBox1.Text);
}
if (op == 1)
{
result=val1+val2;
}
else if (eq == 2)
{
result = val1 - val2;
}
else if (eq == 3)
{
result = val1 * val2;
}
else
{
result = val1 / val2;
}
textBox1.Text = result.ToString();
}
//_______________For Clear text_____________________
private void button18_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
//______________End Equal____________________
//__________________For BackSpace_____________________
private void button17_Click(object sender, EventArgs e)
{
count = textBox1.Text.Length;
str= textBox1.Text;
textBox1.Clear();
for (int i = 0; i < count-1; i++)
{
textBox1.Text = textBox1.Text + str[i];
}
}
//___________________End Backspace_______________________
//_________________For operators_____________
private void button13_Click(object sender, EventArgs e)
{
val1=double.Parse(textBox1.Text);
op = 1;
v= 0;
eq = 1;
}
private void button14_Click(object sender, EventArgs e)
{
val1 = double.Parse(textBox1.Text);
op = 2;
v = 0;
eq = 1;
}
private void button15_Click(object sender, EventArgs e)
{
val1 = double.Parse(textBox1.Text);
op = 3;
v = 0;
eq = 1;
}
private void button16_Click(object sender, EventArgs e)
{
val1 = double.Parse(textBox1.Text);
op = 4;
v = 0;
eq = 1;
}
//_______________________End Operators_____________________
}
Comments
Post a Comment