Trending Tutorials

Visual Studio WinForm Windows Calculator Tutorial Example (C#)



This video demonstrates how to create a winform application using Visual Studio 2012 (but will also work for 2010, 2008, and 2005). The application built is designed to emulate the Windows calculator and is done in C# .Net. It is intended for beginners looking to create their very first winform application.

Incoming search terms:

18 Comments on Visual Studio WinForm Windows Calculator Tutorial Example (C#)

  1. This video was a great help to me sir. Thank you!

  2. I have extracted the icons from calc.exe and combined the two icons that calc.exe uses into one, so use the file provided to get the exact calc.exe look! https://www.dropbox.com/s/759mysdfy4mmx4h/calc.ico?dl=0

  3. Is it possible to build a windows form in VS using c++ instead of c#?

  4. Appreciate your work :)

  5. I really like how easy that was to build. I would like to take advantage of it, but how difficult is it to convert C# application to run on other operating systems?

  6. Thank you sir Chris Merritt.

  7. Hey man, fantastic tutorial, I have only 1 critique.  You forgot to make sure that the clear button also clears the label.

  8. Eu fiz umas modificações que acabam por aceitar a incrementação do cálculo, não sendo apenas a soma de dois valores.
    private void operador_click(object sender, EventArgs e)
            {
                Button b = (Button)sender;
                operacao_pressionado = true;
                labelequacao.Text = labelequacao.Text + " " + textResultado.Text;
                switch (operacao)
                {
                    case "+":
                        valorsomado = (valorsomado + Double.Parse(textResultado.Text));
                        textResultado.Text = valorsomado.ToString();
                        break;
                    case "-":
                        valorsomado = (valorsomado – Double.Parse(textResultado.Text));
                        textResultado.Text = valorsomado.ToString();
                        break;
                    case "*":
                        valorsomado = (valorsomado * Double.Parse(textResultado.Text));
                        textResultado.Text = valorsomado.ToString();
                        break;
                    case "/":
                        valorsomado = (valorsomado / Double.Parse(textResultado.Text));
                        textResultado.Text = valorsomado.ToString();
                        break;
                    default:
                        break;
                }
                operacao = b.Text;
                valorsomado = Double.Parse(textResultado.Text);
                labelequacao.Text = labelequacao.Text + " " + operacao;
            }
    private void buttonIgual_Click(object sender, EventArgs e)
            {
    operacao_pressionado = true;
              … (repete o switch acima)
                operacao = "";
                labelequacao.Text = "";
                valorsomado = 0;
            }

  9. Works great, however on my version which I never saw you do… if you put in first number then operation, the clear will not remove that.  C should 0 everything out if it's like a regular calculator.  Everything else is flawless after the tutorial.  Great job!

  10. keep getting errors with case "+";

  11. i am not getting separate digits instead getting a single digit changed when any key is pressed

  12. 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 Calculator
    {
        public partial class Form1 : Form
        {
            double value = 0;
            String operation = "";
            bool operation_pressed = false;

            public Form1()
            {
                InitializeComponent();
            }

            private void button_Click(object sender, EventArgs e)
            {
                if ((result.Text == "0")||(operation_pressed))
                    result.Clear();
                Button b = (Button)sender;
                result.Text = result.Text + b.Text;
            }

            private void button16_Click(object sender, EventArgs e)
            {
                result.Text = "0";
            }

            private void operator_click(object sender, EventArgs e)
            {
                Button b = (Button)sender;
                operation = b.Text;
                value = Double.Parse(result.Text);
                operation_pressed = true;

            }

            private void button18_Click(object sender, EventArgs e)
            {
                switch (operation)
                {
                    case "/":
                        result.Text = (value / Double.Parse(result.Text)).ToString();
                        break;
                    case "*":
                        result.Text = (value * Double.Parse(result.Text)).ToString();
                        break;
                    case "-":
                        result.Text = (value – Double.Parse(result.Text)).ToString();
                        break;
                    case "+":
                        result.Text = (value + Double.Parse(result.Text)).ToString();
                        break;
                    default:
                        break;
                }
                operation_pressed = false;
            }

            private void button17_Click(object sender, EventArgs e)
            {
                result.Clear();
                value = 0;
            }

        }
    }

  13. sir can you please tell my how to publish this calculator i mean to create a setup file

  14. Simply Amazing i tried it an it is amzing

  15. Complete instructions with code:

    Calculator design process                                          
                                                                                               
    Set the calculator up                                      
    Rename textbox1 = “result”                   
    Create a generic "button_Click" event handler for button 1
    "button_Click"
      {
                if (result.Text == "0") // Clears the 0 if a number is typed
                    result.Clear(); 

                Button b = (Button)sender; // Transforms data type into a button
                result.Text = result.Text + b.Text; // Input for the numbers
            }
    Attach “button_Click” to all the buttons (1, 2, 3, 4, 5, 6, 7, 8, 9, 0, .)
    Create a event handler for "CE" button
    "CE"
     {
                result.Text = "0";
            }
    Create a generic "operator_Click" event handler for + button
    Attach "operator_click" to all the operation buttons (/, *, -)
    Create variables
       Double Value = 0; 
       String operation = "";

    "operator_Click"
       Button b = (Button)sender;
                operation = b.Text; // Captures what operation was pressed
                value = Double.Parse(result.Text); // value gets populated by whatever is in the textbox
    Create variables
       bool operation_pressed = false;
    "button_Click"
      Update the previous line
       if ((result.Text == "0")||(operation_pressed = true)) // Resets the textbox||doesnt append to it after the operation is pressed
    Create an event handler for the = button
       switch (operation)//Handles all the math and shows the result on screen
                {
                    case "+":
                        result.Text = (value + Double.Parse(result.Text)).ToString();
                        break;
                    case "-":
                        result.Text = (value – Double.Parse(result.Text)).ToString();
                        break;
                    case "*":
                        result.Text = (value * Double.Parse(result.Text)).ToString();
                        break;
                    case "/":
                        result.Text = (value / Double.Parse(result.Text)).ToString();
                        break;
                    default:
                        break;

                }//end switch
                operation_pressed = false;// resets the outcome back to false
    Create an event handler for the C button
       {
                result.Clear();// rests the value back to 0 (not the full system)
                value = 0;
            }
    Place a label in the top left of the textbox// this will show the number before the operation
    Change the font = 13, BackColor = Window, ForeColor = ButtonShadow
    Change text = , Change name = equation
    operator_Click
       equation.Text = value + " " + operation;
    Above the switch(operation)
       equation.Text = "";
    Move the operation_pressed = false;// resets the outcome back to false
    To button_Click
       {
          if ((result.Text == "0")||(operation_pressed))//Clears the textbox if a number is pressed||stops the number in the textbox being appended to if an operation is pressed
                    result.Clear();

                operation_pressed = false;// resets the outcome back to false
                Button b = (Button)sender; // Transforms data type into a button
                result.Text = result.Text + b.Text; // Input for the numbers
            }

Leave a comment

Your email address will not be published.


*