CREATING A SIMPLE CALCULATOR PROGRAM USING MICROSOFT VISUAL C#.NET PROGRAMMING LANGUAGE TUTORIAL

in #utopian-io7 years ago (edited)

GOOD DAY EVERYONE

This is my first contribution of an open source in the field of programming and technology. To start with, Since I am an computer engineering graduate, I would like to share my knowledge about Microsoft Visual C# .NET Programming that I had learned during my college days. Today I’m going to teach you on how to make a simple calculator using Microsoft Visual c#.net programming language. Though it’s so basic but still it is too useful for those who still newbie in this programming language and for those people that still wondering how a calculator being developed.

FIRST LET’S TALK ABOUT WHAT IS MICROSOFT VISUAL C#.NET?

C# programming language designed for creating a variety of applications that run on the .NET Framework. This language is powerful, type-safe, and object-oriented. They are built on the .NET Compiler Platform “Roslyn” which provides rich code analysis APIs and its all open source on GitHub.

TOOLS NEEDED

BUTTONS
TEXTBOXES
LABELS
RADIO BUTTONS

SOFTWARE USED

Microsoft Visual C#.net 2010 express

LET’S BEGIN OUR TUTORIAL!

First step: Open Microsoft Visual C#.net 2010 express

Once you open the Microsoft Visual C#.net 2010 Express a windows will pop-up then go to menu tab and click “new project”.

A new window will appear, set up your project name and click “Windows Forms Application”, then click the OK button!

Now this is our visual C# working environment. On this window we have to design our form and write codes and debug and run the program.

The Toolbox displays icons for controls and other items that you can add to Visual Studio projects. To open the Toolbox, click Toolbox on the View menu. You can dock the Toolbox.
Toolbox is important in designing your form where you can add label, Button, Textbox, Radio Button, and Etc. Next step is designing our calculator using Toolbox common controls.

This is a Toolbox display window look like.

In creating/designing a calculator we need to go to Toolbox and select the Tools we need.

Tools needed in creating/designing a calculator:
BUTTONS, TEXTBOXES, LABELs, RADIO BUTTONS

Just drag and drop the Tools to your design form environment.

Let us assume that we already created a design for our calculator. Next step is writing a code for our calculator.

In writing codes just double click each button then you will be directly go to writing a code environment.

For example: when you double click “zero button” you will be directly open the form1.cs environment (where here you can write codes for the zero button code)

After double clicking the zero button you will be directly open this environment which is Form1.cs. Here you can write codes for every button.

These are codes that I was made for this tutorial

        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 Calc
        {
           public partial class Form1 : Form
           {
               //DATA TYPES
               double a = 0; 
               double b = 0;
               double c = 0;
               bool equal = false;
               string Oper,no1;
               public Form1()
               {
        
                   InitializeComponent();
                   no1 = ""; 
       
               }
    
    private void CheckIfEqual()
    {
        if(equal)
        {
            textBox1.Text = "";
            equal = false;
        }

    }
    private void b0_Click(object sender, EventArgs e) //ZERO BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "0";
    }

    private void b1_Click(object sender, EventArgs e) //ONE BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "1";
    }

    private void b2_Click(object sender, EventArgs e) //TWO BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "2";
    }

    private void b3_Click(object sender, EventArgs e) //THREE BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "3";
    }

    private void b4_Click(object sender, EventArgs e) //FOUR BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "4";
    }

    private void b5_Click(object sender, EventArgs e) //FIVE BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "5";
    }

    private void b6_Click(object sender, EventArgs e) //SIX BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "6";
    }

    private void b7_Click(object sender, EventArgs e) //SEVEN BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "7";
    }

    private void b8_Click(object sender, EventArgs e) //EIGHT BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "8";
    }

    private void b9_Click(object sender, EventArgs e) //NINE BUTTON CODE
    {
        CheckIfEqual();
        textBox1.Text += "9";
    }

    private void PlusMinus_Click(object sender, EventArgs e) // POSITIVE AND NEGATIVE BUTTON CODE
    {
        if (textBox1.Text.Contains("-"))
        {
            textBox1.Text = textBox1.Text.Remove(0, 1);
        }
        else
        {
            textBox1.Text = "-" + textBox1.Text;
        }
    }

    private void bdot_Click(object sender, EventArgs e) // DOT BUTTON CODE
    {
        CheckIfEqual();
        if (textBox1.Text.Contains("."))
        {
            return;

        }
        else
        {
            textBox1.Text =textBox1.Text + ".";
        }
    }

    private void Backspace_Click(object sender, EventArgs e) //BACKSPACE CODE
    {
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            textBox1.Text = textBox1.Text.Remove(0, 1);
        }
    }

    private void CE_Click(object sender, EventArgs e) //CE OR CLEAR ENTRY BUTTON CODE
    {
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            textBox1.Text = "";
        }
    }

    private void Plus_Click(object sender, EventArgs e) //ADDITION OR PLUS BUTTON CODE
    {
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            a += Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
            Oper = "+";
        }
    }

    private void Equal_Click(object sender, EventArgs e) // EQUAL BUTTON CODE
    {
        equal=true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            switch (Oper)
            {
                case ("+"):
                    b += Convert.ToDouble(textBox1.Text);
                    c = a + b;
                    textBox1.Text = c.ToString();
                    break;
                case ("-"):
                    b += Convert.ToDouble(textBox1.Text);
                    c = a - b;
                    textBox1.Text = c.ToString();
                    break;
                case ("*"):
                    b += Convert.ToDouble(textBox1.Text);
                    c = a * b;
                    textBox1.Text = c.ToString();
                    break;
                case ("/"):
                    b += Convert.ToDouble(textBox1.Text);
                    c = a / b;
                    textBox1.Text = c.ToString();
                    break;
                case ("x^y"):
                    textBox1.Text = Convert.ToString(System.Math.Pow(Convert.ToDouble(no1)
                                  , Convert.ToDouble(textBox1.Text)));
                    break; 
                case ("%"):
                    textBox1.Text = Convert.ToString(Convert.ToDouble(no1) % Convert.ToDouble(textBox1.Text));
                    break;
            }
        }
        a = 0;
        b = 0;
        c = 0;
    }

    private void Minus_Click(object sender, EventArgs e) //MINUS OR SUBSTRACTION BUTTON CODE
    {
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            a += Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
            Oper = "-";
        }
    }

    private void Mul_Click(object sender, EventArgs e) //MULTIPLICATION BUTTON CODE
    {
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            a += Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
            Oper = "*";
        }
    }

    private void div_Click(object sender, EventArgs e) //DIVISION OR DIVIDE BUTTON CODE
    {
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            a += Convert.ToDouble(textBox1.Text);
            textBox1.Text = "";
            Oper = "/";
        }
    }

    private void Clear_Click(object sender, EventArgs e) //CLEAR BUTTON CODE
    {
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            Oper = "";
            textBox1.Text = "";
            a = b = c = 0;
           
        }
    }

    private void Sqrt_Click(object sender, EventArgs e) //SQUAREROOT BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            
            textBox1.Text = Convert.ToString(System.Math.Sqrt(Convert.ToDouble(textBox1.Text)));
        }
    }

    private void Xinv_Click(object sender, EventArgs e) //X INVERT BUTTON CODE
    {

        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            textBox1.Text = Convert.ToString(Convert.ToDouble(1.0 / Convert.ToDouble(textBox1.Text)));
        }
    }

    private void Percent_Click(object sender, EventArgs e) // PERCENT BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            no1 = textBox1.Text;
            textBox1.Text = "";
            Oper = "%";
            
        }
    }

    private void ON_Click(object sender, EventArgs e) // AC OR ON BUTTON CODE
    {

            label2.Text = "ON";
            label3.Text = "    ARTELIE JOHN YLANAN";
            textBox1.BackColor = Color.SkyBlue;
            textBox1.Enabled = true;
            textBox1.Text = "";
            b0.Enabled = true;
            b1.Enabled = true;
            b2.Enabled = true;
            b3.Enabled = true;
            b4.Enabled = true;
            b5.Enabled = true;
            b6.Enabled = true;
            b7.Enabled = true;
            b8.Enabled = true;
            b9.Enabled = true;
            PlusMinus.Enabled = true;
            bdot.Enabled = true;
            Backspace.Enabled = true;
            CE.Enabled = true;
            Plus.Enabled = true;
            Equal.Enabled = true;
            Minus.Enabled = true;
            Mul.Enabled = true;
            div.Enabled = true;
            Clear.Enabled = true;
            Sqrt.Enabled = true;
            Xinv.Enabled = true;
            Percent.Enabled = true;
            PiB.Enabled = true;
            sinB.Enabled = true;
            cosB.Enabled = true;
            tanB.Enabled = true;
            sinInv.Enabled = true;
            cosInv.Enabled = true;
            tanInv.Enabled = true;
            RadB.Enabled = true;
            DegB.Enabled = true;
            x2.Enabled = true;
            x3.Enabled = true;
            pictureBox1.Enabled = true;
            pictureBox2.Enabled = true;
            xy.Enabled = true;
            button1.Enabled = true;
        
    }

    private void button1_Click(object sender, EventArgs e) //OFF BUTTON CODE
    {

            label2.Text = "OFF";
            label3.Text = "PRESS AC BUTTON TO   ON";
            textBox1.BackColor = Color.SteelBlue;
            textBox1.Enabled = false;
            textBox1.Text = "";
            b0.Enabled = false;
            b1.Enabled = false;
            b2.Enabled = false;
            b3.Enabled = false;
            b4.Enabled = false;
            b5.Enabled = false;
            b6.Enabled = false;
            b7.Enabled = false;
            b8.Enabled = false;
            b9.Enabled = false;
            PlusMinus.Enabled = false;
            bdot.Enabled = false;
            Backspace.Enabled = false;
            CE.Enabled = false;
            Plus.Enabled = false;
            Equal.Enabled = false;
            Minus.Enabled = false;
            Mul.Enabled = false;
            div.Enabled = false;
            Clear.Enabled = false;
            Sqrt.Enabled = false;
            Xinv.Enabled = false;
            Percent.Enabled = false;
            PiB.Enabled = false;
            sinB.Enabled = false;
            cosB.Enabled = false;
            tanB.Enabled = false;
            sinInv.Enabled = false;
            cosInv.Enabled = false;
            tanInv.Enabled = false;
            RadB.Enabled = false;
            DegB.Enabled = false;
            x2.Enabled = false;
            x3.Enabled = false;
            pictureBox1.Enabled = false;
            pictureBox2.Enabled = false;
            xy.Enabled = false;
            button1.Enabled = false;
    }

    private void PiB_Click(object sender, EventArgs e) // PI BUTTON CODE
    {

        textBox1.Text = "3.141592654"; 
    }

    private void sinB_Click(object sender, EventArgs e) //SINE BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            //if radian is selected - RADIO BUTTON CODE
            if (RadB.Checked == true)
            {
                textBox1.Text = Convert.ToString(System.Math.Sin
                (Convert.ToDouble(textBox1.Text)));
            }
            //if degree is selected - RADIO BUTTON CODE
            else
            {
                textBox1.Text = Convert.ToString(System.Math.Sin(
                (Convert.ToDouble(System.Math.PI) / 180) * (Convert.ToDouble(textBox1.Text))));
            }
        }
    }

    private void cosB_Click(object sender, EventArgs e) //COSINE BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            //if radian is selected - RADIO BUTTON CODE
            if (RadB.Checked == true)
            {
                textBox1.Text = Convert.ToString(System.Math.Cos
                (Convert.ToDouble(textBox1.Text)));
            }
            //if degree is selected - RADIO BUTTON CODE
            else
            {
                textBox1.Text = Convert.ToString(System.Math.Cos(
                (Convert.ToDouble(System.Math.PI) / 180) * (Convert.ToDouble(textBox1.Text))));
            }
        }
    }

    private void tanB_Click(object sender, EventArgs e) //TANGENT BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            //if radian is selected - RADIO BUTTON CODE
            if (RadB.Checked == true)
            {
                textBox1.Text = Convert.ToString(System.Math.Tan
                (Convert.ToDouble(textBox1.Text)));
            }
            //if degree is selected - RADIO BUTTON CODE
            else
            {
                textBox1.Text = Convert.ToString(System.Math.Tan(
                (Convert.ToDouble(System.Math.PI) / 180) * (Convert.ToDouble(textBox1.Text))));
            }
        }
    }

    private void sinInv_Click(object sender, EventArgs e) //SINE INVERT BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            //if radian is selected - RADIO BUTTON CODE
            if (RadB.Checked == true)
            {
                textBox1.Text = Convert.ToString(System.Math.Asin
                (Convert.ToDouble(textBox1.Text)));
            }
            //if degree is selected - RADIO BUTTON CODE
            else
            {
                textBox1.Text = Convert.ToString(System.Math.Asin(
                (Convert.ToDouble(System.Math.PI) / 180) * (Convert.ToDouble(textBox1.Text))));
            }
        }
    }

    private void cosInv_Click(object sender, EventArgs e) //COSINE INVERT BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            //if radian is selected - RADIO BUTTON CODE
            if (RadB.Checked == true)
            {
                textBox1.Text = Convert.ToString(System.Math.Acos
                (Convert.ToDouble(textBox1.Text)));
            }
            //if degree is selected - RADIO BUTTON CODE
            else
            {
                textBox1.Text = Convert.ToString(System.Math.Acos(
                (Convert.ToDouble(System.Math.PI) / 180) * (Convert.ToDouble(textBox1.Text))));
            }
        }
    }

    private void tanInv_Click(object sender, EventArgs e) //TANGENT INVERSE BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            //if radian is selected - RADIO BUTTON CODE
            if (RadB.Checked == true)
            {
                textBox1.Text = Convert.ToString(System.Math.Atan
                (Convert.ToDouble(textBox1.Text)));
            }
            //if degree is selected - RADIO BUTTON CODE
            else
            {
                textBox1.Text = Convert.ToString(System.Math.Atan(
                (Convert.ToDouble(System.Math.PI) / 180) * (Convert.ToDouble(textBox1.Text))));
            }
        }
    }

    private void x2_Click(object sender, EventArgs e) //RAISE TO THE POWER OF TWO BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            textBox1.Text = Convert.ToString(Convert.ToDouble(textBox1.Text)
                          * Convert.ToDouble(textBox1.Text));
        }
    }

    private void x3_Click(object sender, EventArgs e) //RAISE TO THE POWER OF THREE BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            textBox1.Text = Convert.ToString(Convert.ToDouble(textBox1.Text)
                          * Convert.ToDouble(textBox1.Text)
                          * Convert.ToDouble(textBox1.Text));
        }
    }

    private void xy_Click(object sender, EventArgs e) //RAISE TO THE POWER OF Y BUTTON CODE
    {
        equal = true;
        if (textBox1.Text == "")
        {
            return;
        }
        else
        {
            no1 = textBox1.Text;
            textBox1.Text = "";
            Oper = "x^y";
        }
    }

    }
    }

To run the program click debugging or press F5 to start debugging

Our calculator will pop up.
At first all the buttons of our calculator were disabled to enable the button just press the AC button to ON our calculator.

This is what it look like when our calculator is ON!

Here I attached a video so that you can watch the final result of our simple calculator program using Microsoft Visual C#.net 2010 express programming language.

Thank you for visiting, reading and watching my tutorial. Please subscribe for more.
Comments and Suggestions are free and open.

PLEASE FOLLOW, UPVOTE AND RESTEEM AUTHOR: @artjan

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.029
BTC 76535.07
ETH 2962.73
USDT 1.00
SBD 2.65