Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Thursday, 8 October 2015

Calculator Program In C#

 No comments   

Introduction

A basic Calculator program which is written in C# by using various Window Form Controls.
Calculator performs Addition, Subtraction, Division, Multiplication , Square Root, Power and find Absolute value on given input numbers.

Calculator Design View On Window Form


C# Code(On Code-Behind Window)

namespace Calculator1ByJatin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        double value = 0;
        double no2 = 0;
        bool OperatorPress = false;
        string operationPerformed = "";
               // Function Check For Point 
        public bool CheckForType()
        {
            char[] arr = txtmsg.Text.ToCharArray();
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == '.')
                    return true;
            }
            return false;
        }

        private void BtnAll(object sender, EventArgs e)
        {
            if ((txtmsg.Text == "0") || (OperatorPress))
                txtmsg.Clear();

                OperatorPress = false;
                Button b = (Button)sender;
                txtmsg.Text += b.Text;
        }

       private void btnCE_Click(object sender, EventArgs e)
        {
            txtmsg.Text = "0";
            value = 0;
            label2.Text = "";
        }
                 // Single event for all alphatbets.
        private void BtnAllOperator(object sender, EventArgs e)
        {
            if (value != 0)
            {
                btnequals.PerformClick();
                Button b = (Button)sender;
                operationPerformed = b.Text;
                value = Convert.ToDouble(txtmsg.Text);
                label2.Text += value + " " + operationPerformed;
                OperatorPress = true;
              //  pow = true;
            }
            else
            {
                Button b = (Button)sender;
                operationPerformed = b.Text;
                value = Convert.ToDouble(txtmsg.Text);
             label2.Text += value + " " + operationPerformed;
                OperatorPress = true;
            //    pow = true;
            }
         
        }

        private void btnZero_Click(object sender, EventArgs e)
        {
            if (txtmsg.Text != "0")
                txtmsg.Text += btnZero.Text;
            else
                txtmsg.Text = btnZero.Text;
        }

        private void btnDot_Click(object sender, EventArgs e)
        {
            if (txtmsg.Text == "")
                txtmsg.Text = "0.";
            else
                if (!CheckForType())
                    txtmsg.Text += btnDot.Text;
        }


        private void btnequals_Click(object sender, EventArgs e)
        {
            switch(operationPerformed)
            {
                case "+" :
                   
            txtmsg.Text= (value+Convert.ToDouble(txtmsg.Text)).ToString();
                break;
                case "-":
             txtmsg.Text=(value-Convert.ToDouble(txtmsg.Text)).ToString();
             break;
                case "*":
                    txtmsg.Text=(value*Convert.ToDouble(txtmsg.Text)).ToString();
                break;
                case "/":
                    txtmsg.Text=(value/Convert.ToDouble(txtmsg.Text)).ToString();
                break;
                case "Power":
                txtmsg.Text = (Math.Pow(value, Convert.ToDouble(txtmsg.Text))).ToString();
                break;
            }
            value = Convert.ToDouble(txtmsg.Text);
           
            label2.Text = "";
        }

        private void btnOff_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnback_Click(object sender, EventArgs e)
        {
            txtmsg.Text = txtmsg.Text.Remove(txtmsg.Text.Length - 1);
        }
   
     
        private void btnsqrt_Click(object sender, EventArgs e)
        {
            value = double.Parse(txtmsg.Text);
            no2 = Math.Sqrt(value);
            txtmsg.Text = no2.ToString();
            no2 = 0;
        }

        private void btnAbs_Click(object sender, EventArgs e)
        {

            value = double.Parse(txtmsg.Text);
            no2 = Math.Truncate(value);
            txtmsg.Text = no2.ToString();
            no2 = 0;
        }
        double m = 0;

        private void btnmplus_Click(object sender, EventArgs e)
        {
            m = double.Parse(txtmsg.Text);
            if(txtmsg.Text!="0")
            label4.Text = "M";
          
        }

        private void btnmr_Click(object sender, EventArgs e)
        {
            if (m > 0)
                txtmsg.Text = m.ToString();
        }

        private void btnmc_Click(object sender, EventArgs e)
        {
            m = 0;
            label4.Text = "";
        }
     
    }
}
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write