• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to make a Calculator in C#

EL CAMALEON

Chibi Collector
E Rep
0
0
0
Rep
0
E Vouches
0
0
0
Vouches
0
Posts
66
Likes
17
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
In this tutorial, I will show you how to create a calculator using C#.net. This is a simple calculator with functions that works accurately. It also contains procedures that are very easy to follow and can be done in a short period of time.

Let's begin:

Step 1. Open Microsoft Visual Studio 2008 and create new Windows Form Application for C#. After that, do the form just like this.
calcsarpfig.1asdas.png


Step 2. Go to the Solution Explorer, double-click the “View Code” to fire the code editor.
calcsarpfig.2asdas.png

Step 3. In the code editor, declare the variables that are needed.
  1. double

    total =

    0

    ;
  2. string

    LOperator;

Step 4. Set the following codes for the function of the (0-9 ,clear and .) buttons .
  1. private

    void

    btn1_Click(

    object

    sender, EventArgs e)
  2. {
  3. if

    (

    txtView.

    Text

    ==

    "0"

    )
  4. {
  5. txtView.

    Clear

    (

    )

    ;
  6. txtView.

    Text

    +=

    btn1.

    Text

    ;
  7. }
  8. else
  9. {
  10. txtView.

    Text

    +=

    btn1.

    Text

    ;
  11. }
  12. }

  13. private

    void

    btn2_Click(

    object

    sender, EventArgs e)
  14. {
  15. if

    (

    txtView.

    Text

    ==

    "0"

    )
  16. {
  17. txtView.

    Clear

    (

    )

    ;
  18. txtView.

    Text

    +=

    btn2.

    Text

    ;
  19. }
  20. else
  21. {
  22. txtView.

    Text

    +=

    btn2.

    Text

    ;
  23. }
  24. }

  25. private

    void

    btn3_Click(

    object

    sender, EventArgs e)
  26. {
  27. if

    (

    txtView.

    Text

    ==

    "0"

    )
  28. {
  29. txtView.

    Clear

    (

    )

    ;
  30. txtView.

    Text

    +=

    btn3.

    Text

    ;
  31. }
  32. else
  33. {
  34. txtView.

    Text

    +=

    btn3.

    Text

    ;
  35. }
  36. }

  37. private

    void

    btn0_Click(

    object

    sender, EventArgs e)
  38. {
  39. if

    (

    txtView.

    Text

    ==

    "0"

    )
  40. {
  41. txtView.

    Clear

    (

    )

    ;
  42. txtView.

    Text

    +=

    btn0.

    Text

    ;
  43. }
  44. else
  45. {
  46. txtView.

    Text

    +=

    btn0.

    Text

    ;
  47. }
  48. }

  49. private

    void

    btn8_Click(

    object

    sender, EventArgs e)
  50. {
  51. if

    (

    txtView.

    Text

    ==

    "0"

    )
  52. {
  53. txtView.

    Clear

    (

    )

    ;
  54. txtView.

    Text

    +=

    btn8.

    Text

    ;
  55. }
  56. else
  57. {
  58. txtView.

    Text

    +=

    btn8.

    Text

    ;
  59. }
  60. }

  61. private

    void

    btn7_Click(

    object

    sender, EventArgs e)
  62. {
  63. if

    (

    txtView.

    Text

    ==

    "0"

    )
  64. {
  65. txtView.

    Clear

    (

    )

    ;
  66. txtView.

    Text

    +=

    btn7.

    Text

    ;
  67. }
  68. else
  69. {
  70. txtView.

    Text

    +=

    btn7.

    Text

    ;
  71. }

  72. }

  73. private

    void

    btn9_Click(

    object

    sender, EventArgs e)
  74. {
  75. if

    (

    txtView.

    Text

    ==

    "0"

    )
  76. {
  77. txtView.

    Clear

    (

    )

    ;
  78. txtView.

    Text

    +=

    btn9.

    Text

    ;
  79. }
  80. else
  81. {
  82. txtView.

    Text

    +=

    btn9.

    Text

    ;
  83. }

  84. }

  85. private

    void

    btn6_Click(

    object

    sender, EventArgs e)
  86. {
  87. if

    (

    txtView.

    Text

    ==

    "0"

    )
  88. {
  89. txtView.

    Clear

    (

    )

    ;
  90. txtView.

    Text

    +=

    btn6.

    Text

    ;
  91. }
  92. else
  93. {
  94. txtView.

    Text

    +=

    btn6.

    Text

    ;
  95. }

  96. }

  97. private

    void

    btn5_Click(

    object

    sender, EventArgs e)
  98. {
  99. if

    (

    txtView.

    Text

    ==

    "0"

    )
  100. {
  101. txtView.

    Clear

    (

    )

    ;
  102. txtView.

    Text

    +=

    btn5.

    Text

    ;
  103. }
  104. else
  105. {
  106. txtView.

    Text

    +=

    btn5.

    Text

    ;
  107. }
  108. }

  109. private

    void

    btn4_Click(

    object

    sender, EventArgs e)
  110. {
  111. if

    (

    txtView.

    Text

    ==

    "0"

    )
  112. {
  113. txtView.

    Clear

    (

    )

    ;
  114. txtView.

    Text

    +=

    btn4.

    Text

    ;
  115. }
  116. else
  117. {
  118. txtView.

    Text

    +=

    btn4.

    Text

    ;
  119. }
  120. }

  121. private

    void

    btnDote_Click(

    object

    sender, EventArgs e)
  122. {
  123. if

    (

    !

    txtView.

    Text

    .

    Contains

    (

    "."

    )

    )
  124. {
  125. txtView.

    Text

    +=

    btnDote.

    Text

    ;
  126. }

  127. }

  128. private

    void

    btnClear_Click(

    object

    sender, EventArgs e)
  129. {
  130. txtView.

    Text

    =

    "0"

    ;
  131. total =

    0

    ;

  132. }

Step 5. Set the following codes for the function of the (x,/,+,-) MDAS calculation.
  1. private

    void

    btnPlus_Click(

    object

    sender, EventArgs e)
  2. {
  3. total +=

    double

    .

    Parse

    (

    txtView.

    Text

    )

    ;
  4. LOperator =

    "plus"

    ;
  5. txtView.

    Clear

    (

    )

    ;


  6. }
  7. private

    void

    btnminus_Click(

    object

    sender, EventArgs e)
  8. {
  9. total +=

    double

    .

    Parse

    (

    txtView.

    Text

    )

    ;
  10. LOperator =

    "minus"

    ;
  11. txtView.

    Clear

    (

    )

    ;

  12. }
  13. private

    void

    btnMultiply_Click(

    object

    sender, EventArgs e)
  14. {
  15. total +=

    double

    .

    Parse

    (

    txtView.

    Text

    )

    ;
  16. LOperator =

    "multiply"

    ;
  17. txtView.

    Clear

    (

    )

    ;
  18. }

  19. private

    void

    btnDivide_Click(

    object

    sender, EventArgs e)
  20. {
  21. total +=

    double

    .

    Parse

    (

    txtView.

    Text

    )

    ;
  22. LOperator =

    "divide"

    ;
  23. txtView.

    Clear

    (

    )

    ;
  24. }
  25. private

    void

    btnEqual_Click(

    object

    sender, EventArgs e)
  26. {

  27. switch

    (

    LOperator)

    {
  28. case

    "plus"

    :
  29. total =

    total +

    double

    .

    Parse

    (

    txtView.

    Text

    )

    ;
  30. txtView.

    Text

    =

    total.

    ToString

    (

    )

    ;
  31. total =

    0

    ;
  32. LOperator =

    ""

    ;
  33. break

    ;
  34. case

    "minus"

    :
  35. total =

    total -

    double

    .

    Parse

    (

    txtView.

    Text

    )

    ;
  36. txtView.

    Text

    =

    total.

    ToString

    (

    )

    ;
  37. total =

    0

    ;
  38. LOperator =

    ""

    ;
  39. break

    ;

  40. case

    "multiply"

    :
  41. total =

    total *

    double

    .

    Parse

    (

    txtView.

    Text

    )

    ;
  42. txtView.

    Text

    =

    total.

    ToString

    (

    )

    ;
  43. total =

    0

    ;
  44. LOperator =

    ""

    ;
  45. break

    ;
  46. case

    "divide"

    :
  47. total =

    total /

    double

    .

    Parse

    (

    txtView.

    Text

    )

    ;
  48. txtView.

    Text

    =

    total.

    ToString

    (

    )

    ;
  49. total =

    0

    ;
  50. LOperator =

    ""

    ;
  51. break

    ;
  52. default

    :
  53. txtView.

    Text

    =

    total.

    ToString

    (

    )

    ;
  54. break

    ;

  55. }




  56. }

Download the complete source code and run it on your computer.

For all students who need a programmer for your thesis system or anyone who needs a source code in any programming languages. You can contact me @ :
Email – [email protected]
Mobile No. – 09305235027 – tnt


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,502

355,786

355,794

Top