Create A Calculator In Windows Form Application

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

  1. Create a Windows Application project from file menu.

    FILE, New, then click Project.

  2. A popup window will appear. Choose “Windows” from templates and choose project “Windows Forms Application.” I have named it “Simple Calculator.”

  3. Create the Form design as follows by using buttons and textbox.

    design

  4. Add 0-9, sign (/,*, -, +, %) buttons, backspace, Clear (C), point (.) buttons.

  5. Add a textbox to show the results.

  6. On each button click use the following login,
    1. private void btn0_Click(object sender, EventArgs e)  
    2. {  
    3.    txtResult.Text = txtResult.Text + "0";  
    4. }  
    Code

  7. On operations buttons (+,-, /,*, %) take care of the following cases.

    1. The signs should not be at 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 Backspace button remove the last character from the textbox.
    4. For 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. Cannot divide by zero.
    8. Cannot find modulus by Zero.

  8. Now we will follow the Microsoft Calculator logic for calculations.

  9. When we press digit sign then again we press sign then result of first expression will be used for next expression i.e.

    5+8+ then result will be 13+ on the result section.

  10. We have to take care of integer numbers as well as float numbers.

  11. For Equal button I have used switch statement.

  12. For more information please see the source code attached as zip.

    run

Thank you.

Read more articles on Windows Forms,

Up Next
    Ebook Download
    View all
    Learn
    View all