Calculator Program/Application using Asp.Net

Creating calculator application is very simple using Asp.Net.
 
Web form: 
  1. <table>  
  2.         <tr>  
  3.             <td colspan="4">  
  4.                  </td>  
  5.         </tr>  
  6.         <tr>  
  7.             <td colspan="4">  
  8.                 <asp:TextBox ID="DisplayTextBox1" runat="server" Height="45px" Width="375px"   
  9.                     TextMode="MultiLine" Font-Bold="True" Font-Size="X-Large" ReadOnly="True"></asp:TextBox>  
  10.             </td>  
  11.         </tr>  
  12.         <tr>  
  13.             <td colspan="4" height="30px">  
  14.                 <asp:Label ID="Answer" runat="server" Font-Bold="True"   
  15.                     Font-Size="X-Large"></asp:Label>  
  16.             </td>  
  17.         </tr>  
  18.         <tr>  
  19.             <td>  
  20.                 <asp:Button ID="Button21" runat="server" Height="50px" Text="("   
  21.                     Width="95px" onclick="Button21_Click" />  
  22.             </td>  
  23.             <td>  
  24.                 <asp:Button ID="Button22" runat="server" Height="50px" Text=")"   
  25.                     Width="95px" onclick="Button22_Click" />  
  26.             </td>  
  27.             <td>  
  28.                 <asp:Button ID="Button24" runat="server" Height="50px" Text="DEL"   
  29.                     Width="95px" onclick="Button24_Click" />  
  30.             </td>  
  31.             <td>  
  32.                 <asp:Button ID="Button25" runat="server" Height="50px" Text="AC"   
  33.                     Width="95px" onclick="Button25_Click" />  
  34.             </td>  
  35.         </tr>  
  36.         <tr>  
  37.             <td>  
  38.                 <asp:Button ID="ButtonNumber7" runat="server" Height="50px" Text="7"   
  39.                     Width="95px" onclick="ButtonNumber7_Click" />  
  40.             </td>  
  41.             <td>  
  42.                 <asp:Button ID="ButtonNumber8" runat="server" Height="50px" Text="8"   
  43.                     Width="95px" onclick="ButtonNumber8_Click" />  
  44.             </td>  
  45.             <td>  
  46.                 <asp:Button ID="ButtonNumber9" runat="server" Height="50px" Text="9"   
  47.                     Width="95px" onclick="ButtonNumber9_Click" />  
  48.             </td>  
  49.             <td>  
  50.                 <asp:Button ID="ButtonNumberDevide" runat="server" Height="50px" Text="÷"   
  51.                     Width="95px"  ForeColor="Black"   
  52.                     onclick="ButtonNumberDevide_Click" />  
  53.             </td>  
  54.         </tr>  
  55.         <tr>  
  56.             <td>  
  57.                 <asp:Button ID="ButtonNumber4" runat="server" Height="50px" Text="4"   
  58.                     Width="95px" onclick="ButtonNumber4_Click" />  
  59.             </td>  
  60.             <td>  
  61.                 <asp:Button ID="ButtonNumber5" runat="server" Height="50px" Text="5"   
  62.                     Width="95px" onclick="ButtonNumber5_Click" />  
  63.             </td>  
  64.             <td>  
  65.                 <asp:Button ID="ButtonNumber6" runat="server" Height="50px" Text="6"   
  66.                     Width="95px" onclick="ButtonNumber6_Click" />  
  67.             </td>  
  68.             <td>  
  69.                 <asp:Button ID="ButtonNumberMulti" runat="server" Height="50px" Text="X"   
  70.                     Width="95px"  onclick="ButtonNumberMulti_Click" />  
  71.             </td>  
  72.         </tr>  
  73.         <tr>  
  74.             <td>  
  75.                 <asp:Button ID="ButtonNumber1" runat="server" Height="50px" Text="1"   
  76.                     Width="95px" onclick="ButtonNumber1_Click" />  
  77.             </td>  
  78.             <td>  
  79.                 <asp:Button ID="ButtonNumber2" runat="server" Height="50px" Text="2"   
  80.                     Width="95px" onclick="ButtonNumber2_Click" />  
  81.             </td>  
  82.             <td>  
  83.                 <asp:Button ID="ButtonNumber3" runat="server" Height="50px" Text="3"   
  84.                     Width="95px" ClientIDMode="AutoID" onclick="ButtonNumber3_Click" />  
  85.             </td>  
  86.             <td>  
  87.                 <asp:Button ID="ButtonNumberMinus" runat="server" Height="50px" Text="_"   
  88.                     Width="95px"  onclick="ButtonNumberMinus_Click" />  
  89.             </td>  
  90.         </tr>  
  91.         <tr>  
  92.             <td>  
  93.                 <asp:Button ID="ButtonNumber0" runat="server" Height="50px" Text="0"   
  94.                     Width="95px" onclick="ButtonNumber0_Click" />  
  95.             </td>  
  96.             <td>  
  97.                 <asp:Button ID="ButtonNumberdot" runat="server" Height="50px" Text="."   
  98.                     Width="95px" onclick="ButtonNumberdot_Click" />  
  99.             </td>  
  100.             <td>  
  101.                 <asp:Button ID="ButtonNumberEqual" runat="server" Height="50px" Text="="   
  102.                     Width="95px"  onclick="ButtonNumberEqual_Click" />  
  103.             </td>  
  104.             <td>  
  105.                 <asp:Button ID="ButtonNumberPlus" runat="server" Height="50px" Text="+"   
  106.                     Width="95px"  onclick="ButtonNumberPlus_Click" />  
  107.             </td>  
  108.         </tr>  
  109.     </table>  
C#:
 
Add the following namespace. 
  1. using System.Data;   
Add the following code inside the .cs page.
  1. protected void Button21_Click(object sender, EventArgs e)    
  2. {    
  3.     DisplayTextBox1.Text = DisplayTextBox1.Text + "(";    
  4. }    
  5.   
  6. protected void Button22_Click(object sender, EventArgs e)    
  7. {    
  8.     DisplayTextBox1.Text = DisplayTextBox1.Text + ")";    
  9. }    
  10.   
  11.   
  12. protected void Button25_Click(object sender, EventArgs e)    
  13. {    
  14.     DisplayTextBox1.Text = "";    
  15.     Answer.Text = "";    
  16. }    
  17.   
  18. protected void Button24_Click(object sender, EventArgs e)    
  19. {    
  20.     string DisplayText = DisplayTextBox1.Text;    
  21.     int LastIndex = DisplayText.Length;    
  22.     DisplayTextBox1.Text = DisplayTextBox1.Text.Remove(LastIndex - 1);    
  23.   
  24. }    
  25.   
  26. protected void ButtonNumberEqual_Click(object sender, EventArgs e)    
  27. {    
  28.   
  29.     try    
  30.     {    
  31.         string Input = DisplayTextBox1.Text;    
  32.         DataTable table = new DataTable();    
  33.         Object answer;    
  34.         answer = table.Compute(Input, null);    
  35.         Answer.Text = answer.ToString();    
  36.     }    
  37.   
  38.     catch    
  39.     {    
  40.         Answer.Text = "ERROR :(";    
  41.     }    
  42.   
  43. }  
Now run the program :) 
 
calculator application jhg