Loading...

How to make a Simple Calculator on Windows Form Application, Step by Step Tutorial

Make a Simple Calculator on Windows Form Application:

we have learned how to Run Your Software only in known or specified Computer on Windows Form Application in C#, Here, We will make a simple calculator on Windows Form Application in C#. The tutorial provided here is very easy step by step and you will most probably easily understand.

Following are the steps to make a Calculator in Windows Form:
  • First of all Open Visual Studio and the Windows Form Application: 

  • Design the Form using Buttons , Combo boxes and Text boxes, as given below, Add Items to the Combo Box,

  • Now go to the code, which you can get by double clicking on any Button, When you do double click on any element of the Form like button on combo box, its event is made in the code class, for example, double click on a button and a method or event will be made in code class and the write the code what you want your application to do when you click on that specific button.
  • We are writing our code below but our advice is to do not copy paste it and write the code in appropriate event.

Code :


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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double mul, div, sub, add;           // initializing variables
            int num1 = Convert.ToInt32(textBox1.Text);   // reading number from text box
            int num2 = Convert.ToInt32(textBox2.Text);
            string op = this.comboBox1.SelectedItem.ToString();  // reading selected item from Combo Box
            mul = Calmul(num1, num2);  // Calling Method of miltiplication
            div = Caldiv(num2 , num1); //
            sub = Calsub(num2, num1);
            add = Caladd(num1, num2);
            if (op == "Add")
            {
                textBox3.Text = add.ToString();  // Writing Answer on text box
            }
            else if (op == "Subtract")
                {
                    textBox3.Text = sub.ToString(); // Writing Answer on text box
                }
                else if (op == "Divide")
                {
                    textBox3.Text = div.ToString(); // Writing Answer on text box
                }
            else if (op == "Multiply")
            {
                textBox3.Text = mul.ToString(); // Writing Answer on text box
            }
        }
        public static double Caladd(int a, int b)  // Method to add two numbers
        {
            double z = 0;
            z = a + b;
            return z;
        }
        public static double Calmul(int a, int b)  // Method to multiply two numbers
        {
            double z = 0;
            z = a * b;
            return z;
        }
        public static double Caldiv(int a, int b)  // Method to divide two numbers
        {
            double z = 0;
            z = a / b;
            return z;
        }
        public static double Calsub(int a, int b)  // Method to subtract two numbers
        {
            double z = 0;
            z = a - b;
            return z;
        }
    }
}

Output: 

The output is already given above in the picture of form.


If you want to want any post about something else, or you want to update it, Kindly Contact us.



Thank You for reading this, Like this if you understood, It helps a lot to make us motivated.

Google+

 
TOP