Monday 26 September 2016

Create Calculator using Windows Form

Tags


Create Calculator using Windows Form


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 calciwindowsapplication
{
    public partial class Form1 : Form
    {
        double result = 0;
        string opration="";
        bool dd=false;
        public Form1()
        {
            InitializeComponent();
        }

   
        private void button18_Click(object sender, EventArgs e)
        {
            Button but = (Button)sender;
            opration = but.Text;
            result = double.Parse(textboxresult.Text);
           operationlabel.Text = result + " " + opration;
         //   textboxresult.Text = result + " " + opration;
           dd = true;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textboxresult.Text = "0";
            result = 0;
        }

        private void button14_Click(object sender, EventArgs e)
        {
     textboxresult.Text = "0";
        }

        private void button24_Click(object sender, EventArgs e)
        {
            switch (opration)
            {
                case "+":
                    textboxresult.Text = (result + double.Parse(textboxresult.Text)).ToString();
                    break;
                case "-":
                    textboxresult.Text = (result - double.Parse(textboxresult.Text)).ToString();
                    break;
                case "*":
                    textboxresult.Text = (result * double.Parse(textboxresult.Text)).ToString();
                    break;
                case "/":
                    textboxresult.Text = (result / double.Parse(textboxresult.Text)).ToString();
                    break;
                default:
                    break;
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Button but = (Button)sender;
            opration = but.Text;
            result = double.Parse(textboxresult.Text);
            operationlabel.Text = result + " " + opration;
            //dd = true;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
               dd = false;
       textboxresult.Text = textboxresult.Text + "6";
        }

     
        private void button20_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
            dd = false;

            //Button but = (Button)sender;
            //textboxresult.Text = textboxresult.Text + but.Text;
            textboxresult.Text = textboxresult.Text + "1";
        }

     
        private void button17_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
                dd = false;
           textboxresult.Text = textboxresult.Text + "3";
        }

     
        private void button16_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
                dd = false;
           textboxresult.Text = textboxresult.Text + "2";
        }

     
        private void button10_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
                dd = false;
          textboxresult.Text = textboxresult.Text + "4";
        }

     
        private void button6_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
                dd = false;
          textboxresult.Text = textboxresult.Text + "5";
        }

     
        private void button15_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
                dd = false;
          textboxresult.Text = textboxresult.Text + "7";
        }

     
        private void button11_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
                dd = false;
        textboxresult.Text = textboxresult.Text + "8";
        }

     
        private void button12_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
                dd = false;
       textboxresult.Text = textboxresult.Text + "9";
        }

     
        private void button25_Click(object sender, EventArgs e)
        {
            if ((textboxresult.Text == "0") || (dd))
                textboxresult.Clear();
               dd = false;
       textboxresult.Text = textboxresult.Text + "0";
        }

     
        private void button22_Click(object sender, EventArgs e)
        {
            //if ((textboxresult.Text == "0") || (dd))
            //    textboxresult.Clear();
            //dd = false;
            if (textboxresult.Text.Contains("."))
            {

                textboxresult.Text = textboxresult.Text;
            }
            else
            {
                textboxresult.Text = textboxresult.Text + ".";
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}
form design



here is output







EmoticonEmoticon