Thursday, June 30, 2011

Sample Calculator with windows form application in c# sharp using visual studio source code

Hi! friends do you want to learn windows form application in visual studio then this sample calculator project may provide hits and idea about event handling in windows form application.Code is written in c#.Here is there file for this copy one by one.
You can also see other sample projects here the lists of other projects.
List of windows form sample projects.




-----------------------------------------------------------------------------

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
 
 
 
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmcalculator());
}
}
}
 






-------------------------------------------------------------------------------------

Form1.Designer.cs


 
namespace WindowsFormsApplication1
{
partial class frmcalculator
{
/// <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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.add = new System.Windows.Forms.Button();
this.multi = new System.Windows.Forms.Button();
this.dot = new System.Windows.Forms.Button();
this.sub = new System.Windows.Forms.Button();
this.eql = new System.Windows.Forms.Button();
this.div = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.off = new System.Windows.Forms.Button();
this.clear = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Lime;
this.button1.Location = new System.Drawing.Point(12, 110);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(26, 27);
this.button1.TabIndex = 0;
this.button1.Text = "1";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(57, 110);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(26, 27);
this.button2.TabIndex = 1;
this.button2.Text = "2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(99, 110);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(26, 27);
this.button3.TabIndex = 2;
this.button3.Text = "3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(12, 162);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(26, 27);
this.button4.TabIndex = 3;
this.button4.Text = "4";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(57, 162);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(26, 27);
this.button5.TabIndex = 4;
this.button5.Text = "5";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(99, 162);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(26, 27);
this.button6.TabIndex = 5;
this.button6.Text = "6";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(12, 212);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(26, 27);
this.button7.TabIndex = 6;
this.button7.Text = "7";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(57, 212);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(26, 27);
this.button8.TabIndex = 7;
this.button8.Text = "8";
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button9
//
this.button9.Location = new System.Drawing.Point(99, 212);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(26, 27);
this.button9.TabIndex = 8;
this.button9.Text = "9";
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(57, 262);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(26, 27);
this.button10.TabIndex = 9;
this.button10.Text = "0";
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// add
//
this.add.Location = new System.Drawing.Point(142, 110);
this.add.Name = "add";
this.add.Size = new System.Drawing.Size(26, 27);
this.add.TabIndex = 10;
this.add.Text = "+";
this.add.UseVisualStyleBackColor = true;
this.add.Click += new System.EventHandler(this.add_Click);
//
// multi
//
this.multi.Location = new System.Drawing.Point(145, 212);
this.multi.Name = "multi";
this.multi.Size = new System.Drawing.Size(26, 27);
this.multi.TabIndex = 11;
this.multi.Text = "*";
this.multi.UseVisualStyleBackColor = true;
this.multi.Click += new System.EventHandler(this.multi_Click);
//
// dot
//
this.dot.Location = new System.Drawing.Point(12, 262);
this.dot.Name = "dot";
this.dot.Size = new System.Drawing.Size(26, 27);
this.dot.TabIndex = 12;
this.dot.Text = ".";
this.dot.UseVisualStyleBackColor = true;
this.dot.Click += new System.EventHandler(this.dot_Click);
//
// sub
//
this.sub.Location = new System.Drawing.Point(142, 162);
this.sub.Name = "sub";
this.sub.Size = new System.Drawing.Size(26, 27);
this.sub.TabIndex = 13;
this.sub.Text = "-";
this.sub.UseVisualStyleBackColor = true;
this.sub.Click += new System.EventHandler(this.sub_Click);
//
// eql
//
this.eql.Location = new System.Drawing.Point(99, 262);
this.eql.Name = "eql";
this.eql.Size = new System.Drawing.Size(26, 27);
this.eql.TabIndex = 14;
this.eql.Text = "=";
this.eql.UseVisualStyleBackColor = true;
this.eql.Click += new System.EventHandler(this.eql_Click);
//
// div
//
this.div.Location = new System.Drawing.Point(145, 262);
this.div.Name = "div";
this.div.Size = new System.Drawing.Size(26, 27);
this.div.TabIndex = 15;
this.div.Text = "/";
this.div.UseVisualStyleBackColor = true;
this.div.Click += new System.EventHandler(this.div_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 22);
this.textBox1.MaximumSize = new System.Drawing.Size(156, 30);
this.textBox1.Name = "textBox1";
this.textBox1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
this.textBox1.Size = new System.Drawing.Size(156, 20);
this.textBox1.TabIndex = 16;
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBox1.WordWrap = false;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// off
//
this.off.Location = new System.Drawing.Point(99, 65);
this.off.Name = "off";
this.off.Size = new System.Drawing.Size(29, 26);
this.off.TabIndex = 17;
this.off.Text = "off";
this.off.UseVisualStyleBackColor = true;
this.off.Click += new System.EventHandler(this.off_Click);
//
// clear
//
this.clear.Location = new System.Drawing.Point(142, 65);
this.clear.Name = "clear";
this.clear.Size = new System.Drawing.Size(26, 26);
this.clear.TabIndex = 18;
this.clear.Text = "Ac";
this.clear.UseVisualStyleBackColor = true;
this.clear.Click += new System.EventHandler(this.clear_Click);
//
// frmcalculator
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(194, 301);
this.Controls.Add(this.clear);
this.Controls.Add(this.off);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.div);
this.Controls.Add(this.eql);
this.Controls.Add(this.sub);
this.Controls.Add(this.dot);
this.Controls.Add(this.multi);
this.Controls.Add(this.add);
this.Controls.Add(this.button10);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "frmcalculator";
this.Text = "calculator";
this.ResumeLayout(false);
this.PerformLayout();
 
}
 
#endregion
 
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button add;
private System.Windows.Forms.Button multi;
private System.Windows.Forms.Button dot;
private System.Windows.Forms.Button sub;
private System.Windows.Forms.Button eql;
private System.Windows.Forms.Button div;
private System.Windows.Forms.TextBox textBox1;
private int opMain = 0;
private bool isfirst = false;
private bool issecond = false;
private double num1 = 0;
private double num2 = 0;
private double result;
private System.Windows.Forms.Button off;
private System.Windows.Forms.Button clear;
}
}
 





-------------------------------------------------------------------------------------
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 WindowsFormsApplication1
{
public partial class frmcalculator : Form
{

public void docalc(double n1, double n2, int op)
{
result = 0;
setText("clear");
switch (op)
{
case 1:
result = n1 + n2;
break;
case 2:
result = n1 - n2;
break;
case 3:
result = n1 * n2;
break;
case 4:
result = n1 / n2;
break;
}
setText(result.ToString());
}
public void ifequal()
{
num2 = double.Parse(textBox1.Text);
docalc(num1, num2, opMain);
isfirst = false;
}
public void setOperator(int op)
{
string temp;
opMain = op;
num1 = double.Parse(textBox1.Text);
temp = textBox1.Text;
setText("clear");
setText(temp);
issecond = true;
}
public void doequal()
{
num2 = double.Parse(textBox1.Text);
}
 
public void setText(string text)
{
if (text == "clear")
textBox1.Text = "";
else
{
if (issecond)
textBox1.Text = "";
issecond = false;
textBox1.Text += text;
}
}
public frmcalculator()
{
InitializeComponent();
}
 
private void button1_Click(object sender, EventArgs e)
{
//textBox1.Text = "1";
setText("1");

}
 
private void button2_Click(object sender, EventArgs e)
{
// MessageBox.Show("button was clicked");
//if(sender == button1)
 
//textBox1.Text="2";
setText("2");
}
 
private void textBox1_TextChanged(object sender, EventArgs e)
{
 
}
 
private void button3_Click(object sender, EventArgs e)
{
//textBox1.Text = "3";
setText("3");
}
 
private void button4_Click(object sender, EventArgs e)
{
//textBox1.Text = "4";
setText("4");
}
 
private void button5_Click(object sender, EventArgs e)
{
//textBox1.Text = "5";
setText("5");
}
 
private void button6_Click(object sender, EventArgs e)
{
//textBox1.Text = "6";
setText("6");
}
 
private void button7_Click(object sender, EventArgs e)
{
//textBox1.Text = "7";
setText("7");
}
 
private void button8_Click(object sender, EventArgs e)
{
//textBox1.Text = "8";
setText("8");
}
 
private void button9_Click(object sender, EventArgs e)
{
//textBox1.Text = "9";
setText("9");
}
 
private void button10_Click(object sender, EventArgs e)
{
//textBox1.Text = "0";
setText("0");
}
 
private void add_Click(object sender, EventArgs e)
{
//setText("+");
setOperator(1);
}
 
private void sub_Click(object sender, EventArgs e)
{
setOperator(2);
}
 
private void multi_Click(object sender, EventArgs e)
{
setOperator(3);
}
 
private void div_Click(object sender, EventArgs e)
{
setOperator(4);
}
 
private void eql_Click(object sender, EventArgs e)
{
ifequal();
}
 
private void dot_Click(object sender, EventArgs e)
{
setText(".");
setOperator(6);
}
 
private void clear_Click(object sender, EventArgs e)
{
setText("clear");
}
 
private void _Click(object sender, EventArgs e)
{
setText("clear");
}
 
private void off_Click(object sender, EventArgs e)
{
//System.ApplicationException.Exit;
Application.Exit();
}
 
}
}
You can download the complete project file from here.
Download Calculator

19 comments:

  1. Excellent post dude, thanks for sharing this effective article. I hope all information will be found here. Thanks a lot.

    Sample Forms

    ReplyDelete
  2. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
    Vee Eee Technologies

    ReplyDelete
  3. show me output sir ji.......

    ReplyDelete
    Replies
    1. I have provided download for complete project file. So download it run the exe you will get the output.

      Delete
  4. How would you add a divide by zero exception to this code?

    ReplyDelete
    Replies
    1. windows form does by default. It display answer as infinity when divide by zero occur. If you want to show different message then you can add your code for divide by zero exception handling.

      Delete
  5. What if the user clicks the decimal point more than once? How would you include code to show error for this?

    ReplyDelete
    Replies
    1. It doesn't take input decimal point. It just a simple calculator.

      Delete
    2. Remove or comment out that setOperetor(6) in that dot button code and it works perfect

      Delete
  6. Love You and Like This Project..Thank you very much...^^

    ReplyDelete
  7. great work... you could have uploaded the GUI of the calculator!!!!

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Great Bro...............

    ReplyDelete
  10. Could be done so much easier TBH.. e.g. on your Buttons, you have taken each individual button and assigned them to their own EventHandler.. Since all the numeric buttons has the same purpose, (that is, taking whatever the value of the button is and add it to the TextBox) you could add one simple EventHandler to deal with all the Buttons at once, like so:

    private void valueButton_Click(object sender, EventArgs e)
    {
    Button valueButton = (Button)sender;
    resultTextBox.Text += valueButton.Text;
    }

    ReplyDelete
  11. Did someone check this project output? it doesn't work correctly when we need to use decimal(.). please fix this and resend the code

    ReplyDelete