Monday 18 November 2013

Calculator in C# via code


______________

Form1.Designer.cs
_______________

namespace cal
{
partial class Form3
{
/// <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);
}
private void InitializeComponent()
{
this.tboxresult = new System.Windows.Forms.TextBox();
this.btn7 = new System.Windows.Forms.Button();
this.btn8 = new System.Windows.Forms.Button();
this.btn9 = new System.Windows.Forms.Button();
this.btndivide = 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.btnmultiply = new System.Windows.Forms.Button();
this.btn1 = new System.Windows.Forms.Button();
this.btn2 = new System.Windows.Forms.Button();
this.btn3 = new System.Windows.Forms.Button();
this.btnsubtract = new System.Windows.Forms.Button();
this.btn0 = new System.Windows.Forms.Button();
this.btndot = new System.Windows.Forms.Button();
this.btnadd = new System.Windows.Forms.Button();
this.btnequal = new System.Windows.Forms.Button();
this.btnclear = new System.Windows.Forms.Button();
this.SuspendLayout();

this.tboxresult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tboxresult.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.tboxresult.Location = new System.Drawing.Point(12, 12);
this.tboxresult.Multiline = true;
this.tboxresult.Name = "tboxresult";
this.tboxresult.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.tboxresult.Size = new System.Drawing.Size(234, 56);
this.tboxresult.TabIndex = 0;
this.tboxresult.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// btn7
//
this.btn7.Location = new System.Drawing.Point(12, 76);
this.btn7.Name = "btn7";
this.btn7.Size = new System.Drawing.Size(54, 52);
this.btn7.TabIndex = 1;
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(72, 76);
this.btn8.Name = "btn8";
this.btn8.Size = new System.Drawing.Size(54, 52);
this.btn8.TabIndex = 2;
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(132, 76);
this.btn9.Name = "btn9";
this.btn9.Size = new System.Drawing.Size(54, 52);
this.btn9.TabIndex = 3;
this.btn9.Text = "9";
this.btn9.UseVisualStyleBackColor = true;
this.btn9.Click += new System.EventHandler(this.btn9_Click);
//
// btndivide
//
this.btndivide.Location = new System.Drawing.Point(192, 76);
this.btndivide.Name = "btndivide";
this.btndivide.Size = new System.Drawing.Size(54, 52);
this.btndivide.TabIndex = 4;
this.btndivide.Text = "/";
this.btndivide.UseVisualStyleBackColor = true;
this.btndivide.Click += new System.EventHandler(this.btndivide_Click);
//
// btn4
//
this.btn4.Location = new System.Drawing.Point(12, 134);
this.btn4.Name = "btn4";
this.btn4.Size = new System.Drawing.Size(54, 52);
this.btn4.TabIndex = 5;
this.btn4.Text = "4";
this.btn4.UseVisualStyleBackColor = true;
this.btn4.Click += new System.EventHandler(this.btn4_Click);
//
// btn5
//
this.btn5.Location = new System.Drawing.Point(72, 134);
this.btn5.Name = "btn5";
this.btn5.Size = new System.Drawing.Size(54, 52);
this.btn5.TabIndex = 6;
this.btn5.Text = "5";
this.btn5.UseVisualStyleBackColor = true;
this.btn5.Click += new System.EventHandler(this.btn5_Click);
//
// btn6
//
this.btn6.Location = new System.Drawing.Point(132, 134);
this.btn6.Name = "btn6";
this.btn6.Size = new System.Drawing.Size(54, 52);
this.btn6.TabIndex = 7;
this.btn6.Text = "6";
this.btn6.UseVisualStyleBackColor = true;
this.btn6.Click += new System.EventHandler(this.btn6_Click);
//
// btnmultiply
//
this.btnmultiply.Location = new System.Drawing.Point(192, 134);
this.btnmultiply.Name = "btnmultiply";
this.btnmultiply.Size = new System.Drawing.Size(54, 52);
this.btnmultiply.TabIndex = 8;
this.btnmultiply.Text = "*";
this.btnmultiply.UseVisualStyleBackColor = true;
this.btnmultiply.Click += new System.EventHandler(this.btnmultiply_Click);
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(12, 192);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(54, 52);
this.btn1.TabIndex = 9;
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(72, 192);
this.btn2.Name = "btn2";
this.btn2.Size = new System.Drawing.Size(54, 52);
this.btn2.TabIndex = 10;
this.btn2.Text = "2";
this.btn2.UseVisualStyleBackColor = true;
this.btn2.Click += new System.EventHandler(this.btn2_Click);
//
// btn3
//
this.btn3.Location = new System.Drawing.Point(132, 192);
this.btn3.Name = "btn3";
this.btn3.Size = new System.Drawing.Size(54, 52);
this.btn3.TabIndex = 11;
this.btn3.Text = "3";
this.btn3.UseVisualStyleBackColor = true;
this.btn3.Click += new System.EventHandler(this.btn3_Click);
//
// btnsubtract
//
this.btnsubtract.Location = new System.Drawing.Point(192, 192);
this.btnsubtract.Name = "btnsubtract";
this.btnsubtract.Size = new System.Drawing.Size(54, 52);
this.btnsubtract.TabIndex = 12;
this.btnsubtract.Text = "-";
this.btnsubtract.UseVisualStyleBackColor = true;
this.btnsubtract.Click += new System.EventHandler(this.btnsubtract_Click);
//
// btn0
//
this.btn0.Location = new System.Drawing.Point(12, 250);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(114, 52);
this.btn0.TabIndex = 13;
this.btn0.Text = "0";
this.btn0.UseVisualStyleBackColor = true;
this.btn0.Click += new System.EventHandler(this.btn0_Click);
//
// btndot
//
this.btndot.Location = new System.Drawing.Point(132, 250);
this.btndot.Name = "btndot";
this.btndot.Size = new System.Drawing.Size(54, 52);
this.btndot.TabIndex = 15;
this.btndot.Text = ".";
this.btndot.UseVisualStyleBackColor = true;
this.btndot.Click += new System.EventHandler(this.btndot_Click);
//
// btnadd
//
this.btnadd.Location = new System.Drawing.Point(192, 250);
this.btnadd.Name = "btnadd";
this.btnadd.Size = new System.Drawing.Size(54, 110);
this.btnadd.TabIndex = 16;
this.btnadd.Text = "+";
this.btnadd.UseVisualStyleBackColor = true;
this.btnadd.Click += new System.EventHandler(this.btnadd_Click);
//
// btnequal
//
this.btnequal.Location = new System.Drawing.Point(12, 308);
this.btnequal.Name = "btnequal";
this.btnequal.Size = new System.Drawing.Size(114, 52);
this.btnequal.TabIndex = 17;
this.btnequal.Text = "=";
this.btnequal.UseVisualStyleBackColor = true;
this.btnequal.Click += new System.EventHandler(this.btnequal_Click);
//
// btnclear
//
this.btnclear.Location = new System.Drawing.Point(132, 308);
this.btnclear.Name = "btnclear";
this.btnclear.Size = new System.Drawing.Size(54, 52);
this.btnclear.TabIndex = 18;
this.btnclear.Text = "Clear";
this.btnclear.UseVisualStyleBackColor = true;
this.btnclear.Click += new System.EventHandler(this.btnclear_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Lavender;
this.ClientSize = new System.Drawing.Size(258, 370);
this.Controls.Add(this.btnclear);
this.Controls.Add(this.btnequal);
this.Controls.Add(this.btnadd);
this.Controls.Add(this.btndot);
this.Controls.Add(this.btn0);
this.Controls.Add(this.btnsubtract);
this.Controls.Add(this.btn3);
this.Controls.Add(this.btn2);
this.Controls.Add(this.btn1);
this.Controls.Add(this.btnmultiply);
this.Controls.Add(this.btn6);
this.Controls.Add(this.btn5);
this.Controls.Add(this.btn4);
this.Controls.Add(this.btndivide);
this.Controls.Add(this.btn9);
this.Controls.Add(this.btn8);
this.Controls.Add(this.btn7);
this.Controls.Add(this.tboxresult);
this.Name = "Form1";
this.Text = "Calculator";
this.ResumeLayout(false);
this.PerformLayout();

}

private System.Windows.Forms.TextBox tboxresult;
private System.Windows.Forms.Button btn7;
private System.Windows.Forms.Button btn8;
private System.Windows.Forms.Button btn9;
private System.Windows.Forms.Button btndivide;
private System.Windows.Forms.Button btn4;
private System.Windows.Forms.Button btn5;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btnmultiply;
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Button btnsubtract;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btndot;
private System.Windows.Forms.Button btnadd;
private System.Windows.Forms.Button btnequal;
private System.Windows.Forms.Button btnclear;
}
}

___________________
Form1.cs
__________________

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

namespace cal
{
public partial class Form3 : Form
{
double doublevalue1 = 0;
double doublevalue2 = 0;

string stroperatorsign;

public Form3()
{
InitializeComponent();
}

private void btn1_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn1.Text;
}

private void btn2_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn2.Text;
}

private void btn3_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn3.Text;
}

private void btn4_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn4.Text;
}

private void btn5_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn5.Text;
}

private void btn6_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn6.Text;
}

private void btn7_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn7.Text;
}

private void btn8_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn8.Text;
}

private void btn9_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn9.Text;
}

private void btn0_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btn0.Text;
}

private void btndot_Click(object sender, EventArgs e)
{
tboxresult.Text = tboxresult.Text + btndot.Text;
}

private void btnclear_Click(object sender, EventArgs e)
{
tboxresult.Clear();
}

private void btnadd_Click(object sender, EventArgs e)
{
doublevalue1 = doublevalue1 + double.Parse(tboxresult.Text.ToString());
tboxresult.Clear();

stroperatorsign = "+";
}

private void btnsubtract_Click(object sender, EventArgs e)
{
doublevalue1 = doublevalue1 + double.Parse(tboxresult.Text.ToString());
tboxresult.Clear();

stroperatorsign = "-";
}

private void btnmultiply_Click(object sender, EventArgs e)
{
doublevalue1 = doublevalue1 + double.Parse(tboxresult.Text.ToString());
tboxresult.Clear();

stroperatorsign = "*";
}

private void btndivide_Click(object sender, EventArgs e)
{
doublevalue1 = doublevalue1 + double.Parse(tboxresult.Text.ToString());
tboxresult.Clear();

stroperatorsign = "/";
}

private void btnequal_Click(object sender, EventArgs e)
{

switch (stroperatorsign)
{
case "+":
doublevalue2 = doublevalue1 + double.Parse(tboxresult.Text.ToString());
break;

case "-":
doublevalue2 = doublevalue1 - double.Parse(tboxresult.Text.ToString());
break;

case "*":
doublevalue2 = doublevalue1 * double.Parse(tboxresult.Text.ToString());
break;

case "/":
doublevalue2 = doublevalue1 / double.Parse(tboxresult.Text.ToString());
break;
}

tboxresult.Text = doublevalue2.ToString();
doublevalue1=0;
}
}
}

Yash

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 H@cking Tricks.