Here is a procedure to create a basic calculator. In this section I will build a basic calculator that can perform the following operations:

Steps:

Open Microsoft Visual Studio 2010 then click on "File" -> "New" -> "Website".

Calculator-in-Csharp-1.jpg

Then select "ASP.NET Empty Website".

Calculator-in-Csharp-2.jpg

Then "Add New Item":

Calculator-in-Csharp-3.jpg

Then select "Web Form":

Calculator-in-Csharp-4.jpg

And click "Add". Now it will open the page as in the following:

Calculator-in-Csharp-5.jpg

Here we will write our code:.

Process to create calculator

First we need to create a TextBox and 16 Buttons as in the following:

Here is the code for the "Default.aspx" page:

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head id="Head1" runat="server">
  3. <title></title>
  4. <style type="text/css">
  5. .cal
  6. {
  7. position:absolute;
  8. top:50px;
  9. left:150px;
  10. right:400px;
  11. height:500px;
  12. bottom:100px;
  13. background-color:Teal;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <form id="form1" runat="server">
  19. <div class="cal">
  20. <asp:Label ID="l" Text=" BASIC CALCULATOR" runat="server" Style="margin-left: 200px"
  21. Font-Bold="False" Font-Italic="False"></asp:Label>
  22. <asp:TextBox ID="t" runat="server" Style="margin-left: 100px; margin-top: 24px;"
  23. Width="335px" Height="41px"></asp:TextBox>
  24. <asp:Button ID="b1" Text="1" runat="server" Height="37px" Style="margin-left: 0px"
  25. Width="57px" OnClick="b1_Click" />
  26. <asp:Button ID="b2" Text="2" runat="server" Height="37px" Style="margin-left: 0px"
  27. Width="57px" OnClick="b2_Click" />
  28. <asp:Button ID="b3" Text="3" runat="server" Height="37px" Style="margin-left: 0px"
  29. Width="57px" OnClick="b3_Click" />
  30. <asp:Button ID="add" Text="+" runat="server" Height="37px" Style="margin-left: 0px;
  31. margin-top: 0px;" Width="57px" OnClick="add_Click" />
  32. <asp:Button ID="b4" Text="4" runat="server" Height="37px" Style="margin-left: 0px"
  33. Width="57px" OnClick="b4_Click" />
  34. <asp:Button ID="b5" Text="5" runat="server" Height="37px" Style="margin-left: 0px"
  35. Width="57px" OnClick="b5_Click" />
  36. <asp:Button ID="b6" Text="6" runat="server" Height="37px" Style="margin-left: 0px"
  37. Width="57px" OnClick="b6_Click" />
  38. <asp:Button ID="sub" Text="-" runat="server" Height="37px" Style="margin-left: 0px"
  39. Width="57px" OnClick="sub_Click" />
  40. <asp:Button ID="b7" Text="7" runat="server" Height="37px" Style="margin-left: 0px"
  41. Width="57px" OnClick="b7_Click" />
  42. <asp:Button ID="b8" Text="8" runat="server" Height="37px" Style="margin-left: 0px"
  43. Width="57px" OnClick="b8_Click" />
  44. <asp:Button ID="b9" Text="9" runat="server" Height="37px" Style="margin-left: 0px"
  45. Width="57px" OnClick="b9_Click" />
  46. <asp:Button ID="mul" Text="*" runat="server" Height="37px" Style="margin-left: 0px"
  47. Width="57px" OnClick="mul_Click" />
  48. <asp:Button ID="b0" runat="server" Text="0" Height="37px" Style="margin-left: 0px"
  49. Width="57px" OnClick="b0_Click" />
  50. <asp:Button ID="clr" runat="server" Text="CLR" Height="37px" Style="margin-left: 0px"
  51. Width="57px" OnClick="clr_Click" />
  52. <asp:Button ID="eql" runat="server" Text="=" Height="37px" Style="margin-left: 0px"
  53. Width="57px" OnClick="eql_Click" />
  54. <asp:Button ID="div" Text="/" runat="server" Height="37px" Style="margin-left: 0px"
  55. Width="57px" OnClick="div_Click" />
  56. </div>
  57. </form>
  58. </body>
  59. </html>

Here is the code for the "Default.aspx.cs" page (the code for all the buttons) :

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. public partial class _Default : System.Web.UI.Page
  8. {
  9. static float a, c, d;
  10. static char b;
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void b1_Click(object sender, EventArgs e)
  15. {
  16. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  17. {
  18. t.Text = "";
  19. t.Text = t.Text + b1.Text;
  20. }
  21. else
  22. t.Text = t.Text + b1.Text;
  23. }
  24. protected void b2_Click(object sender, EventArgs e)
  25. {
  26. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  27. {
  28. t.Text = "";
  29. t.Text = t.Text + b2.Text;
  30. }
  31. else
  32. t.Text = t.Text + b2.Text;
  33. }
  34. protected void b3_Click(object sender, EventArgs e)
  35. {
  36. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  37. {
  38. t.Text = "";
  39. t.Text = t.Text + b3.Text;
  40. }
  41. else
  42. t.Text = t.Text + b3.Text;
  43. }
  44. protected void b4_Click(object sender, EventArgs e)
  45. {
  46. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  47. {
  48. t.Text = "";
  49. t.Text = t.Text + b4.Text;
  50. }
  51. else
  52. t.Text = t.Text + b4.Text;
  53. }
  54. protected void b5_Click(object sender, EventArgs e)
  55. {
  56. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  57. {
  58. t.Text = "";
  59. t.Text = t.Text + b5.Text;
  60. }
  61. else
  62. t.Text = t.Text + b5.Text;
  63. }
  64. protected void b6_Click(object sender, EventArgs e)
  65. {
  66. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  67. {
  68. t.Text = "";
  69. t.Text = t.Text + b6.Text;
  70. }
  71. else
  72. t.Text = t.Text + b5.Text;
  73. }
  74. protected void b7_Click(object sender, EventArgs e)
  75. {
  76. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  77. {
  78. t.Text = "";
  79. t.Text = t.Text + b7.Text;
  80. }
  81. else
  82. t.Text = t.Text + b7.Text;
  83. }
  84. protected void b8_Click(object sender, EventArgs e)
  85. {
  86. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  87. {
  88. t.Text = "";
  89. t.Text = t.Text + b8.Text;
  90. }
  91. else
  92. t.Text = t.Text + b8.Text;
  93. }
  94. protected void b9_Click(object sender, EventArgs e)
  95. {
  96. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  97. {
  98. t.Text = "";
  99. t.Text = t.Text + b9.Text;
  100. }
  101. else
  102. t.Text = t.Text + b9.Text;
  103. }
  104. protected void b0_Click(object sender, EventArgs e)
  105. {
  106. if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))
  107. {
  108. t.Text = "";
  109. t.Text = t.Text + b0.Text;
  110. }
  111. else
  112. t.Text = t.Text + b0.Text;
  113. }
  114. protected void add_Click(object sender, EventArgs e)
  115. {
  116. a = Convert.ToInt32(t.Text);
  117. t.Text = "";
  118. b = '+';
  119. t.Text += b;
  120. }
  121. protected void sub_Click(object sender, EventArgs e)
  122. {
  123. a = Convert.ToInt32(t.Text);
  124. t.Text = "";
  125. b = '-';
  126. t.Text += b;
  127. }
  128. protected void mul_Click(object sender, EventArgs e)
  129. {
  130. a = Convert.ToInt32(t.Text);
  131. t.Text = "";
  132. b = '*';
  133. t.Text += b;
  134. }
  135. protected void div_Click(object sender, EventArgs e)
  136. {
  137. a = Convert.ToInt32(t.Text);
  138. t.Text = "";
  139. b = '/';
  140. t.Text += b;
  141. }
  142. protected void eql_Click(object sender, EventArgs e)
  143. {
  144. c = Convert.ToInt32(t.Text);
  145. t.Text = "";
  146. if (b == '/')
  147. {
  148. d = a / c;
  149. t.Text += d;
  150. a = d;
  151. }
  152. else if (b == '+')
  153. {
  154. d = a + c;
  155. t.Text += d;
  156. a = d;
  157. }
  158. else if (b == '-')
  159. {
  160. d = a - c;
  161. t.Text += d;
  162. a = d;
  163. }
  164. else
  165. {
  166. d = a * c;
  167. t.Text += d;
  168. a = d;
  169. }
  170. }
  171. protected void clr_Click(object sender, EventArgs e)
  172. {
  173. t.Text = "";
  174. }
  175. }

The final output looks as in the following:

Calculator-in-Csharp-7.jpg