Create A Calculator In Windows Form Application

Here are the steps to create a simple calculator in Windows Forms.

  • Create a Windows Application project from the file menu.
  • FILE, New, then click Project.
  • A popup window will appear. Choose “Windows” from the templates and choose the project “Windows Forms Application.” I have named it “Simple Calculator.”
  • Create the form design using buttons and text boxes.
    Textbox
  • Add 0-9, sign (/,*, -, +, %) buttons, backspace, Clear (C), and point (.) buttons.
  • Add a textbox to show the results.
  • On each button, click use the following login.
    private void btn0_Click(object sender, EventArgs e)
    {
        txtResult.Text = txtResult.Text + "0";
    }
    
    Code
  • On operations buttons (+,-, /,*, %) take care of the following cases.
    1. The signs should not be in first place in the textbox i.e. +1, /2 etc.
    2. If you press point(.), then in add zero before point i.e. 0.9
    3. For the Backspace button, remove the last character from the textbox.
    4. For the C button clear the text of the text box.
    5. The signs should not appear twice i.e. 4++,9**
    6. The point should not come twice i.e. 2.3.5
    7. It cannot be divided by zero.
    8. Cannot find modulus by Zero.
  • Now, we will follow the Microsoft Calculator logic for calculations.
  • When we press the digit sign, then again we press the sign, the result of the first expression will be used for the next expression i.e.
    • 5+8+, and then the result will be 13+ in the result section.
  • We have to take care of integer numbers as well as float numbers.
  • For the Equal button, I have used the switch statement.
  • For more information, please see the source code attached as zip.
    run

Thank you.

Read more articles on Windows Forms.


Similar Articles