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

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » Custom Controls » Basics 
14. 2. 14. Calculator component based on page component (VB.net)
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(/ 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.