Wednesday, July 3, 2019

Making A Simple Calculator Using C# & Visual Studio

Hello Everyone and Welcome to BYTE PLEASE! DCoder here I have been a coding enthusiast all my life and I like to share what I have learned because the only thing which expands with sharing is knowledge. So today I will try to teach you how to make a Simple Calculator using C# & Visual Studio.



Simple Calculator Using C# and Visual Studio
You can download this 161 KB executable file software here.



I would like to mention a few points before we start, so that you can consume this post a little better.
  • Don't ignore the words in BOLD letters because they are important.
  • It will be a guide and not a sample project. I will tell you everything about this project and even give you a detailed description of each step.
  • You should have a Visual Studio installation and basic knowledge of C# is necessary.

Step 1: Getting Started

  • Open your edition of Visual Studio the go to File > New > Project.
  • After you click on project you will get the following New Project window:
  • Select Visual C# > Windows Forms App then give a suitable name. I have kept mine as Calculator change your location or go with default. Click OK when you think you are done.

Step 2: Knowing The Essentials

  • The first thing you will notice will be the Design window. It is the place where you will determine the look of your Application, a simple calculator in this case.
  • The next important thing will be the Solution Explorer which keeps track of all files and give you easy access to all of them. Don't worry if you cant find it use this shortcut Ctrl + Alt + L.
  • Next comes the Toolbox which is the most essential for this project as we can drag and drop a button, textbox or label and many more directly to the form design. The shortcut is Ctrl + Alt + X.
  • Finally, we need the Properties to set the attribute values for the tools and the form. The Properties is divided into two sections left side consists of Attributes and the right side consists of the Values. The two most important attributes for this project are Name [it is the attribute by which it will be called inside the program], Text [it is the text which is shown in the tool]. The shortcut is F4.

Step 3: Let's Dig In

(I will be giving the properties based on my project, you can give any name you want but for starters I would recommend you to follow me closely. The values are given in Aqua color.)
You will need to add this tools with the following properties inside this form:
  • Click on the Form itself to set its properties first.
    • Name: Calc
    • Text: Calculator
  • A Textbox for taking input and showing the result.
    • Name: tb1
    • Delete the value inside of Text and leave it blank. 
  •  An '=' Button for calculating the result.
    • Name: equalto
    • Text: =
  • A '+' Button for adding.
    • Name: plus
    • Text: +
  • A '-' Button for substracting.
    • Name: minus
    • Text: -
  • A 'x' Button for multiplying.
    • Name: multiply
    • Text: x
  • A '/' Button for division.
    • Name: divide
    • Text: /
  • A '%' Button for modulus operation.
    • Name: percent
    • Text: %
  • A '^' Button for exponent operation.
    • Name: expo
    • Text: ^
  •  0-input Button.
    • Name: zero
    • Text: 0
  • 1-input Button.
    • Name: one
    • Text: 1
  • 2-input Button.
    • Name: two
    • Text: 2
  • 3-input Button.
    • Name: three
    • Text: 3
  • 4-input Button.
    • Name: four
    • Text: 4
  • 5-input Button.
    • Name: five
    • Text: 5
  • 6-input Button.
    • Name: six
    • Text: 6
  • 7-input Button.
    • Name: seven
    • Text: 7
  • 8-input Button.
    • Name: eight
    • Text: 8
  • 9-input Button.
    • Name: nine
    • Text: 9
  • '.' Button for inserting decimal point
    • Name: dot
    • Text: .
  • clear Button for clearing the textbox of previous results or inputs.
    • Name: clear
    • Text: clear
  • .00-Button for inserting two zeroes after the decimal point. (This button is optional)
    • Name: zerodo
    • Text: .00

Step 4: Start To Code

  • Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
    public partial class Calc : Form
    {
        string input = string.Empty;        //string storing user input
        string operand1 = string.Empty;     //string storing first operand
        string operand2 = string.Empty;     //string storing second operand
        char operation;                     //char for operation
        double result = 0.0;                //calculated result
        public Calc()
        {
            InitializeComponent();
        }
        private void zero_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "0";
            this.tb1.Text += input;
        }
        private void one_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "1";
            this.tb1.Text += input;
        }
        private void two_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "2";
            this.tb1.Text += input;
        }
        private void three_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "3";
            this.tb1.Text += input;
        }
        private void four_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "4";
            this.tb1.Text += input;
        }
        private void five_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "5";
            this.tb1.Text += input;
        }
        private void six_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "6";
            this.tb1.Text += input;
        }
        private void seven_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "7";
            this.tb1.Text += input;
        }
        private void eight_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "8";
            this.tb1.Text += input;
        }
        private void nine_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += "9";
            this.tb1.Text += input;
        }
        private void zerodo_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += ".00";
            this.tb1.Text += input;
        }
        private void dot_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            input += ".";
            this.tb1.Text += input;
        }
        private void plus_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '+';
            input = string.Empty;
        }
        private void percent_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '%';
            input = string.Empty;
        }
        private void minus_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '-';
            input = string.Empty;
        }
        private void divide_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '/';
            input = string.Empty;
        }
        private void multiply_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = 'x';
            input = string.Empty;
        }
        private void expo_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operation = '^';
            input = string.Empty;
        }
        private void clear_Click(object sender, EventArgs e)
        {
            this.tb1.Text = "";
            this.input = string.Empty;
            this.operand1 = string.Empty;
            this.operand2 = string.Empty;
        }
        private void equalto_Click(object sender, EventArgs e)
        {
            operand2 = input;
            double num1, num2;
            double.TryParse(operand1, out num1);
            double.TryParse(operand2, out num2);
            if(operation == '+')
            {
                result = num1 + num2;
                tb1.Text = result.ToString();
                //for continuos calculations
                input = tb1.Text;
                operand1 = input;
                operand2 = string.Empty;
            }
            else if(operation == '-')
            {
                result = num1 - num2;
                tb1.Text = result.ToString();
                input = tb1.Text;
                operand1 = input;
                operand2 = string.Empty;
            }
            else if(operation == '/')
            {
                result = num1 / num2;
                tb1.Text = result.ToString();
                input = tb1.Text;
                operand1 = input;
                operand2 = string.Empty;
            }
            else if(operation == '%')
            {
                result = num1 % num2;
                tb1.Text = result.ToString();
                input = tb1.Text;
                operand1 = input;
                operand2 = string.Empty;
            }
            else if(operation == 'x')
            {
                result = num1 * num2;
                tb1.Text = result.ToString();
                input = tb1.Text;
                operand1 = input;
                operand2 = string.Empty;
            }
            else
            {
                result = 1.0;
                for(int i=0; i< num2; i++)
                {
                    result *= num1;
                }
                tb1.Text = result.ToString();
                input = tb1.Text;
                operand1 = input;
                operand2 = string.Empty;
            }
        }
        //for taking only number input from keyboard
        private void tb1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}

Step 5: Debug and Release

  • At first debug your code and check if the application is running successfully. Then you can release your application. The released application is located in the Project Folder > Bin > Release.

Did you complete the project successfully? What hardships you faced? Did you like this project? 

Please COMMENT, LIKE & FOLLOW.. Show your support and I will show you how to make a simple game based on Chess. Thank You!!





No comments:

Post a Comment

Eight Queens Puzzle Game using C# and Visual Studio

Hello Everyone and Welcome to BYTE PLEASE ! DCoder here and I have brought to you my second post which is a simple game based on chess kno...