Calculator component based on page component (VB.net) : Basics « Custom Controls « ASP.NET Tutorial






File: index.aspx

<%@ Page language="VB" %>
<%@ Register TagPrefix="Java2sASP" TagName="Calculator" src="Control.ascx" %>

<html><body>
   <form runat="server">
      <Java2sASP:Calculator id="Calc1" runat="server"/>
   </form>
</body></html>

File: Control.ascx
<script language="VB" runat="server">
   private answer as double
   
   Sub btOperator_Click(Sender as Object, e as EventArgs)
      if (hiddenvalue.value <> "" and hiddenvalue.value <> "0")
         tbNumber.Text = Operate(Sender.Text, hiddenvalue.value, tbNumber.Text)
         hiddenvalue.value = ""
         hiddenoperator.value = ""
      else
         hiddenvalue.value = tbNumber.Text
         hiddenoperator.value = Sender.Text
         tbNumber.Text = "0"
      end if
   End Sub
   
   Sub btSpecOperator_Click(Sender as Object, e as EventArgs)
      tbNumber.Text = Operate(Sender.Text, hiddenvalue.value, tbNumber.Text)
      hiddenvalue.value = ""
      hiddenoperator.value = ""
   End Sub
   
   Sub btNumber_Click(Sender as Object, e as EventArgs)
      response.write(answer.ToString)
      if tbNumber.Text <> "0" then
         tbNumber.Text = tbNumber.Text & Sender.Text
      else
         tbNumber.Text = Sender.Text
         answer = nothing
      end if
   end sub
   
   Sub btClear_Click(Sender as Object, e as EventArgs)
      tbNumber.Text = "0"
      hiddenoperator.value = ""
      hiddenvalue.value = ""
   end sub
   
   Sub btBS_Click(Sender as Object, e as EventArgs)
      if tbNumber.Text <> "0" then
         tbNumber.Text = Left(tbNumber.Text, len(tbNumber.Text)-1) 
      end if
   end sub
   
   Sub btMemory_Click(Sender as Object, e as EventArgs)
      select case Sender.text
         case "MC"
            Session("memory") = ""
            Indicator.Text = ""
         case "MS"
            Session("memory") = tbNumber.Text
            Indicator.Text = "M"
         case "MR"
            tbNumber.Text = Session("Memory")
         case "M+"
            
      end select
   end sub
   
   Sub btEqual_Click(Sender as object, e as EventArgs)
      if hiddenvalue.value <> "" then
         if hiddenoperator.value <> "" then
            tbNumber.Text = Operate(hiddenoperator.value, hiddenvalue.value, tbNumber.Text)
            hiddenvalue.value = ""
            hiddenoperator.value = ""
            answer = tbNumber.Text
         else
            'do nothing
         end if
      else
         'do nothing
      end if
   end sub
   
   private function Operate(op as string, number1 as string, number2 as string ) as double
      select case op
         case "+"
            Operate = CDbl(number1) + CDbl(number2)
         case "-"
            Operate = CDbl(number1) - CDbl(number2)
         case "*"
            Operate = CDbl(number1) * CDbl(number2)
         case "/"
            Operate = CDbl(number1) / CDbl(number2)
         case "sqrt"
            Operate = CDbl(Math.sqrt(number2))
         case "1/x"
            Operate = CDbl(1 / cdbl(number2))
         case "+/-"
            Operate = CDbl(-cdbl(number2))
      end select
   end function
</script>   

<asp:Panel id="Frame" runat="server">
   <table width="250">
   <tr>
      <td width="100%" colspan="7">
         <asp:textbox id="tbNumber" runat=server
            text="0"
            enabled="false"
            width="100%" />
      </td>
   </tr>
   <tr>
      <td align="right" width="100%" colspan="7">
         <asp:Label id="Indicator" text="" runat="server" />
         <asp:button id="btBS" Text="Backspace" 
            OnClick="btBS_Click" runat=server width="75"/>
         <asp:button id="btClear" Text="C" 
            OnClick="btClear_Click" runat=server width="35"/>
      </td>
   </tr>  
   <tr>
      <td width="32" valign="top">
         <asp:button id="btMC" Text="MC" 
            OnClick="btMemory_Click" runat=server 
            width="35"/>
      </td>
      <td width="58"></td>
      <td width="32">
         <asp:button id="bt7" Text="7" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="bt8" Text="8" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="bt9" Text="9" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btDivide" Text="/" 
            OnClick="btOperator_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btSqrt" Text="sqrt" 
            OnClick="btSpecOperator_Click" runat=server 
            width="35"/>
      </td>
   </tr>
   <tr>
      <td width="32" valign="top">
         <asp:button id="btMR" Text="MR" 
            OnClick="btMemory_Click" runat=server 
            width="35"/>
      </td>
      <td width="8"></td>
      <td width="32">
         <asp:button id="bt4" Text="4" 
            OnClick="btNumber_Click" 
            runat=server width="35"/>   
      </td>
      <td width="32">
         <asp:button id="bt5" Text="5" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="bt6" Text="6" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btMultiply" Text="*" 
            OnClick="btOperator_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btPercent" Text="%" 
            OnClick="btOperator_Click" runat=server 
            width="35"/>
      </td>
   </tr>  
   <tr>
      <td width="32" valign="top">
         <asp:button id="btMS" Text="MS" 
            OnClick="btMemory_Click" runat=server 
            width="35"/>
      </td>
      <td width="8"></td>
      <td width="32">
         <asp:button id="bt1" Text="1" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>   
      </td>
      <td width="32">
         <asp:button id="bt2" Text="2" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="bt3" Text="3" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btSubtract" Text="-" 
            OnClick="btOperator_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btOneOver" Text="1/x" 
            OnClick="btSpecOperator_Click" runat=server 
            width="35"/>
      </td>
   </tr> 
   <tr>
      <td width="32" valign="top">
         <asp:button id="btMP" Text="M+" 
            OnClick="btMemory_Click" runat=server 
            width="35"/>
      </td>
      <td width="8"></td>
      <td width="32">
         <asp:button id="bt0" Text="0" 
            OnClick="btNumber_Click" runat=server 
            width="35"/>   
      </td>
      <td width="32">
         <asp:button id="btNegate" Text="+/-" 
            OnClick="btSpecOperator_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btDot" Text="." 
            OnClick="btNumber_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btAdd" Text="+" 
            OnClick="btOperator_Click" runat=server 
            width="35"/>
      </td>
      <td width="32">
         <asp:button id="btEqual" Text="=" 
            OnClick="btEqual_Click" runat=server 
            width="35"/>
      </td>
   </tr> 
   </table>
</asp:Panel>
<input type="hidden" id="hiddenvalue" value="" 
   runat="server" >
<input type="hidden" id="hiddenoperator" value="" 
   runat="server" >








14.2.Basics
14.2.1.User control with property
14.2.2.ASP controls in user control
14.2.3.A Web user control file template
14.2.4.Adding controls to the Web user control
14.2.5.Creating control events in a user control (C#)
14.2.6.Creating control events in a user control (VB)
14.2.7.Exposing user control properties (C#)
14.2.8.Exposing user control properties (VB)
14.2.9.Populating user control properties from the host Web page (C#)
14.2.10.Populating user control properties from the host Web page (VB)
14.2.11.Component level variable
14.2.12.Action from user-defined control
14.2.13.Pass parameter from user-defined controls to host page
14.2.14.Calculator component based on page component (VB.net)
14.2.15.Day minder page component
14.2.16.Use custom control in Assembly
14.2.17.Building a simple User control that randomly displays one image
14.2.18.Registering User Controls in the Web Configuration File
14.2.19.Building Fully Rendered Controls