⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.ascx

📁 asp入门到精通的源代码
💻 ASCX
字号:
<script language="VB" runat="server">
   private answer as double
   
   Sub btOperator_Click(obj as object, e as eventargs)
      if (hiddenvalue.value <> "" and hiddenvalue.value <> "0")
         tbNumber.Text = Operate(obj.Text, hiddenvalue.value, tbNumber.Text)
         hiddenvalue.value = ""
         hiddenoperator.value = ""
      else
         'save old number as hidden input field
         ' and clear text box
         hiddenvalue.value = tbNumber.Text
         hiddenoperator.value = obj.Text
         tbNumber.Text = "0"
      end if
   End Sub
   
   Sub btSpecOperator_Click(obj as object, e as eventargs)
      tbNumber.Text = Operate(obj.Text, hiddenvalue.value, tbNumber.Text)
      hiddenvalue.value = ""
      hiddenoperator.value = ""
   End Sub
   
   Sub btNumber_Click(obj as object, e as eventargs)
      response.write(answer.ToString)
      if tbNumber.Text <> "0" then
         tbNumber.Text = tbNumber.Text & obj.Text
      else
         tbNumber.Text = obj.Text
         answer = nothing
      end if
   end sub
   
   Sub btClear_Click(obj as object, e as eventargs)
      tbNumber.Text = "0"
      hiddenoperator.value = ""
      hiddenvalue.value = ""
   end sub
   
   Sub btBS_Click(obj 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(obj as object, e as eventargs)
      select case obj.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(obj as object, e as eventargs)
      if hiddenvalue.value <> "" then
         if hiddenoperator.value <> "" then
            'operate numbers
            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(operator as string, number1 as string, optional number2 as string = "1") as double
      select case operator
         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"/><p>
      </td>
      <td width="32">
         <asp:button id="bt8" Text="8" 
            OnClick="btNumber_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="bt9" Text="9" 
            OnClick="btNumber_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btDivide" Text="/" 
            OnClick="btOperator_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btSqrt" Text="sqrt" 
            OnClick="btSpecOperator_Click" runat=server 
            width="35"/><p>
      </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"/><p>   
      </td>
      <td width="32">
         <asp:button id="bt5" Text="5" 
            OnClick="btNumber_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="bt6" Text="6" 
            OnClick="btNumber_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btMultiply" Text="*" 
            OnClick="btOperator_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btPercent" Text="%" 
            OnClick="btOperator_Click" runat=server 
            width="35"/><p>
      </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"/><p>   
      </td>
      <td width="32">
         <asp:button id="bt2" Text="2" 
            OnClick="btNumber_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="bt3" Text="3" 
            OnClick="btNumber_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btSubtract" Text="-" 
            OnClick="btOperator_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btOneOver" Text="1/x" 
            OnClick="btSpecOperator_Click" runat=server 
            width="35"/><p>
      </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"/><p>   
      </td>
      <td width="32">
         <asp:button id="btNegate" Text="+/-" 
            OnClick="btSpecOperator_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btDot" Text="." 
            OnClick="btNumber_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btAdd" Text="+" 
            OnClick="btOperator_Click" runat=server 
            width="35"/><p>
      </td>
      <td width="32">
         <asp:button id="btEqual" Text="=" 
            OnClick="btEqual_Click" runat=server 
            width="35"/><p>
      </td>
   </tr> 
   </table>
</asp:Panel>
<input type="hidden" id="hiddenvalue" value="" 
   runat="server" >
<input type="hidden" id="hiddenoperator" value="" 
   runat="server" >

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -