.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Simple calculator in c#.net

Posted By:laptop charles       Posted Date: October 15, 2010    Points: 15    Category: Visual Studio    URL: http://www.dotnetspark.com  

It just calculate the simple mathematics calculation like addition,subtraction,multiplication and division in windows application.
 

It contains 17 buttons like 0-9 numbers and one is '=' symbol,one is dot ".",one is clear button "clear", four button is add,sub,mul,div. Then one TextBox it is a display.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace calculator
{
    public partial class Form1 : Form
    {
       double sum = 0;
        double total = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void txtone_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtone.Text;

        }

        private void txttwo_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txttwo.Text;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtthree.Text;
        }

        private void txtfour_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtfour.Text;  
        }

        private void txtfive_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtfive.Text;
        }

        private void txtsix_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtsix.Text; 
        }

        private void txtseven_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtseven.Text;
           
        }

        private void txteight_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txteight.Text;
        }

        private void txtnine_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtnine.Text;
        }

        private void txtadd_Click(object sender, EventArgs e)
        {
            sum = sum + double.Parse(txtdisplay.Text);
            txtdisplay.Clear();  
        }

        private void txtclear_Click(object sender, EventArgs e)
        {
            txtdisplay.Clear();
        }

        private void txtequel_Click(object sender, EventArgs e)
        {
            total  = sum + double.Parse(txtdisplay.Text);
            txtdisplay.Text = total.ToString();
            sum = 0;
        }

        private void txtdot_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtdot.Text;
        }

        private void txtzero_Click(object sender, EventArgs e)
        {
            txtdisplay.Text = txtdisplay.Text + txtzero.Text;
        }

        private void btnsub_Click(object sender, EventArgs e)
        {
            sum = sum - double.Parse(txtdisplay.Text);
            txtdisplay.Clear();
            
        }

        private void txtmul_Click(object sender, EventArgs e)
        {
            sum = sum * double.Parse(txtdisplay.Text);
            txtdisplay.Clear();
        }

        private void txtdiv_Click(object sender, EventArgs e)
        {
            sum = sum / double.Parse(txtdisplay.Text);
            txtdisplay.Clear();  
        }
               
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend