Simple Calculator using C#

Posted by Samath
  124890
September 24, 2012

 
Hello world! its ur boy Samath once again. Today I am going to show you how to build a Calculator. It is recommended that you get the visual studio environment. if you don't understand the code don't be afraid to ask questions by leaving a comment below.

please don't be afraid to ask questions... Open visual studio and give this a try!!!!!!!!!!!!!



using System;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        float num1, ans;
        int count;
 
        private void btnC_Click_1(object sender, EventArgs e)
        {
            textBox1.Clear();
            count = 0; 
        }
 
        private void btnCE_Click(object sender, EventArgs e)
        {
            
            if (num1==0 && textBox1.TextLength>0)
            { 
                num1 = 0; textBox1.Clear();  
            }
            else if (num1 > 0 && textBox1.TextLength > 0)
            { 
                textBox1.Clear();
            }
        
        }
 
        private void btnback_Click(object sender, EventArgs e)
        {
           
            int lenght = textBox1.TextLength-1;
            string text = textBox1.Text;  
            textBox1.Clear();
            for (int i = 0; i < lenght; i++)
                textBox1.Text = textBox1.Text + text[i]; 
        
        }
 
        private void btnminus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                num1 = float.Parse(textBox1.Text);
                textBox1.Clear();
                textBox1.Focus();
                count = 1;
            }
        }
 
        private void btnone_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 1;
        }
 
        private void bttntwo_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 2;
        }
 
        private void btnthree_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 3;
        }
 
        private void btnplus_Click(object sender, EventArgs e)
        {
            num1 = float.Parse(textBox1.Text);
            textBox1.Clear();
            textBox1.Focus();
            count = 2;
        }
 
        private void btnfour_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 4;
        }
 
        private void btnfive_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 5;
        }
 
        private void btnsix_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 6;
        }
 
        private void btnmultiply_Click(object sender, EventArgs e)
        {
            num1 = float.Parse(textBox1.Text);
            textBox1.Clear();
            textBox1.Focus();
            count = 3;
        }
 
        private void btnseven_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 7;
        }
 
        private void btneight_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 8;
        }
 
        private void btnnine_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 9;
        }
 
        private void btndivide_Click(object sender, EventArgs e)
        {
            num1 = float.Parse(textBox1.Text);
            textBox1.Clear();
            textBox1.Focus();
            count = 4; 
        }
 
        private void btnzero_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 0;
        }
 
        private void btnperiod_Click(object sender, EventArgs e)
        {
            int c = textBox1.TextLength;
            int flag = 0;
            string text = textBox1.Text;
            for (int i = 0; i < c; i++)
            { 
                if (text[i].ToString() == ".") 
                { 
                    flag = 1; break; 
                } 
                else 
                { 
                    flag = 0; 
                } 
            }
            if (flag == 0)
            { 
                textBox1.Text = textBox1.Text + "."; 
            }
        }
 
        private void btnequal_Click(object sender, EventArgs e)
        {
            compute(count);
        }
 
        public void compute(int count)
        {
            switch (count)
            {
                case 1:
                    ans = num1 - float.Parse(textBox1.Text);
                    textBox1.Text = ans.ToString();
                    break;
                case 2:
                    ans = num1 + float.Parse(textBox1.Text);
                    textBox1.Text = ans.ToString();
                    break;
                case 3:
                    ans = num1 * float.Parse(textBox1.Text);
                    textBox1.Text = ans.ToString();
                    break;
                case 4:
                    ans = num1 / float.Parse(textBox1.Text);
                    textBox1.Text = ans.ToString();
                    break;
                default:
                    break;
            }
        }
       
     }
}
   
  
 
 
   

 
Gizmosis350k
September 24, 2012
 
The power of Events and OOP, simply beautiful T_T
Samath
September 24, 2012
 
true!
Samath
September 25, 2012
 
Here is the link to download the source code for this file. Enjoy!
https://docs.google.com/open?id=0B4PXJilumj_INktzaHlFUUx5LTA
JanWan
September 25, 2012
 
This code seem interesting & a little complex, but it's great. I can't wait 2  start once more again in di world of programming. The key of computing is within our hands, the future generation of programming... di code above sell off...
Samath
September 25, 2012
 
respect mi boss.
KingMOW
September 25, 2012
 
highly rated bro..!!! 
Samath
September 25, 2012
 
Thanks
KingMOW
September 28, 2012
 
very creative...
Xingjuan
October 11, 2012
 
great!
trisetya94
April 23, 2014
 
thanks gan.
DeathSpecter9
December 20, 2015
 
Thanks for this Tutorial.
Micjohn
June 25, 2016
 
Sir can i ask a question about visual c#?? how to solve an age in c#??
Micjohn
June 25, 2016
 
I need it because its my assignment can you answer it??? plz
azpxex
March 28, 2017
 
Why when i click the button like 1,2,3 it wont appear on the textbox? Anyone help me please. Thanks