Martes, Enero 3, 2012

Simple Calculator in C#


 

This is a simple calculator using c# language and windows form that allows user to perform the four mathematical operation. Follow the steps below:

1. Create a new Windows Form and name it Simple Calculator.

2. Put 15 Buttons and 1 Textbox.

3. Change the name of the buttons.  
  Label      Name
      0       zeroButton 
      1       oneButton 
      2       twoButton  
      3       threeButton 
      4       fourButton
      5       fiveButton
      6       sixButton
      7       sevenButton
      8       eightButton
      9       nineButton
      +      addButton
       -      subtractButton
       *      multiplyButton
       /      divideButton
       .      dotButton
      =      equalsButton
   CLR   clearButton

For the Textbox name it "outputTextbox" and make the alignment to the Right.

4. When your done in your design press F7 to go to the code editor.
5. Declare the following class variables.

6. Inside also the Form1 class, type this event handler to prevent repetition of codes.























7. Go back to the Designer View. In every number buttons (0-9), click the event at the properties and at the Click event choose your event handler for the number buttons.



















8. Next make the Calculate parameter. This will hold the code when we use the operation buttons.



























9. Make another event handler for the operation buttons. This code will allow the user in continuous calculations. Example: 1+2+3 = 6.















10. Go to the Designer View again and put the this event to the four operation buttons (+,-,*,/).





















11. Next, double click the equals button and type this code:














12. For the clear button type this code. This code will reset the value the user enter in the calculator.


















13. Lastly run your program and test your Simple Calculator.



 















Challenge:
       Make your own code for the dot button. This will sharpen your brain in the world of coding :))