Schongar P.VBScript unleashed.1997
.pdfMustClear=True
End Sub
Sub MemClear_Click()
memory=0
MustClear=True
End Sub
The code of the Calculator application is fairly straightforward. The actual calculations only require a few lines of code, and most of the code handles the display. You can easily expand the bare bones application presented here to include more operations. For example, you can add a +/- button that inverts the sign of the current number. This button can be implemented with a single line of code:
Display.Caption="-" & Display.Caption
Improving the Calculator
You can easily turn this utility into a scientific calculator. You will spend the most time adjusting the user interface to include more buttons, because the operations are straightforward. For instance, if you add a COS button (for calculating the cosine of a number) on the layout, you must enter the following line in the button's Click event:
Display.Caption=cos(CDbl(Display.Caption))
When the user clicks the COS button, the cosine of the displayed value is calculated and appears on the calculator's display. You can add trigonometric functions, square roots, and powers, because these operations don't require two operands. They just read the current value, calculate a function of the number and display the result. You must, however, add a good deal of error checking code so that the program won't attempt to calculate the square root or logarithm of a negative number.
Another useful variation of the basic Calculator application is a hexadecimal calculator, like the one shown in Figure 23.7. If you add a few more buttons for the hexadecimal digits A through F, you can perform the same operations, only in the hexadecimal number system. In the hexadecimal system, however, you can work with integers only. To perform operations in the hexadecimal system, you must prefix the numbers with the symbols &H and then take their value with the CInt function, which converts its argument to an integer. The arithmetic operations will be carried out in the decimal system and the result gets converted to the hexadecimal system with the function Hex.
The HexCalculator application, shown in Figure 23.7, is a variation on the Calculator that performs hexadecimal calculations. The Equals_Click() subroutine looks slightly different than before:
Sub Equals_Click()
Op2="&H" & Display.Caption
Select Case Op
Case "PLUS":
result = CInt(op1) + CInt(op2)
Case "MINUS":
result = CInt(op1) - CInt(op2)
Case "TIMES":
result = CInt(op1) * CInt(op2)
Case "DIV":
if CInt(op2) = 0 Then
Display.Caption="ERROR!"
else
result = CInt(op1) / CInt(op2)
End If
End Select
If Display.Caption<>"ERROR!" then Display.Caption=Hex(result)
MustClear=True
End Sub
When you prefix the operands with the "&H" string, they get converted to hexadecimal numbers. The CInt() function converts these hex numbers to decimals, performs the operation, and then converts the result back to a hex number before displaying it.
Further Improvements
As you can see, it didn't take much to convert the simple decimal calculator into a hexadecimal one. To summarize, all you had to do was add the extra hex digits, convert the arguments to decimal numbers to carry out the calculations, and then convert the result back to a hexadecimal number to display the result. You can now try to combine both calculators in one by adding a toggle button that switches the mode of the calculator between the two systems. When the calculator is in decimal mode, you should disable the hex digits on the layout and restore them when the calculator is switched back to hexadecimal mode. As far as the calculations are concerned, you can copy both subroutines for the Equals button we presented earlier in your code and call one of them, depending on the calculator's current mode.
Review
In this chapter's examples you were shown how to design applications that perform simple calculations, like converting units between systems, or more complicated calculations like math operations. As you have realized by now, the
functions that do the actual calculations are usually the shortest and simplest part of the application. The real challenge in programming in a visually rich environment like Windows 95 is to provide the simplest, most functional user interface. This requires visual design skills, as well as programming skills.
The code behind the various buttons of the Calculator application wasn't simple, considering what it accomplishes. Arranging the buttons on the layout and coming up with an attractive user interface will probably take you longer than writing the code. After the code of the Calculator application was written, it was surprisingly simple to add support for hexadecimal digits to the application, as well as extend it to handle trigonometric and other advanced math functions.
The Conversions utility is another typical example. Writing the code for converting units between systems is trivial, but with a well-designed user interface we were able to create a usable application (one that doesn't overwhelm the user with options). At the same time, by displaying the conversion factors on the program's form, we were able to design an application that can be expanded without actually touching the code that performs the calculations. You can add more options to the program by simply adding new labels with conversion factors. The logic of converting the units has been built into the application, and you needn't worry about it unless the conversion isn't as simple as a multiplication. The display of the conversion factors on the form not only simplifies the code but provides useful information to the user.
The types of applications we explored in this chapter were rather simple ones, but they are quite common and it's likely that you'll incorporate a similar functionality to some of your applications. No doubt, you've seen numerous applications that perform financial or arithmetic calculations. What will make the difference between yours and the other applications is simplicity and ease of use. Our examples weren't the ultimate examples of simplicity or flexibility, but they demonstrated some of the key features you should incorporate in even the simplest applications.
Chapter 24
WWW Personal Information Manager
by Owen Graupman
CONTENTS
●Overview
●How It Works
●Creating the Page
Working with Cookies
Creating Dynamic HTML
Presenting Information
Tying It All Together
●Review
Overview
Personal information managers have always been one of the most popular applications for a computer. This chapter will show you how to make a World Wide Web-based personal information manager that allows the user to store to-do's, appointments, and e-mail addresses with a few simple clicks of the mouse.
Two very important techniques are used to create this interactive page:
●Dynamic page creation
●Persistent storage through cookies
Dynamic page creation is one of the hottest topics in page creation. A dynamic page changes in response to the environment, user input, or a variety of other information. The WWW PIM uses VBScript to generate the entire page based on the current date and information stored by the user.
Persistent storage through cookies allows visitors to this page to store their appointments in small text files called cookies, which reside on their local computer and can be recalled at any time. Many sites today use cookies to store information about the user. Some of the most common uses are to store color preferences or information provided by the user like their name and address. Because a cookie is just a text file managed by the browser, client-side security is very good. Even if a miscreant person was able to place a virus in a cookie, the file contains headers and footers that would prevent the code from running and infecting your system.
Before you go running off to explore cookies, keep in mind that cookies will only work when the page is loaded from an HTTPD server. Loading this page locally will produce a blank page that will not store any data.
How It Works
When a user loads this page from the Internet, VBScript goes to work parsing through the cookie to pull out information for each of the categories of data. It then formats this information into HTML and writes it directly into the document context as HTML.
When a user clicks on any item, whether it's a to-do, address, or appointment, message boxes prompt the user through creating a new item. The information is then placed into the cookie and the page reloaded.
You can see how the finished example looks in Figure 24.1, and the entire HTML and VBScript code in Listing 24.1.
Figure 24.1 : The finished WWW Personal Information Manager.
Listing 24.1. The WWW Personal Information Manager code listing.
1: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2:
3: <HTML>
4:
5:<HEAD>
6:<TITLE>WWW Personal Information Manager</title>
7:</HEAD>
8:
9:<BODY BGCOLOR="#FFFFFF" LANGUAGE="VBSCRIPT">
10:<H1 ALIGN=CENTER>WWW Personal Information Manager</H1>
11:<P ALIGN=CENTER><FONT SIZE=2>Welcome to the WWW Personal Information Manager.
You can use this site to store to-do's, appointments,
12: and email addresses. This information is stored on your local computer via
cookies. To add, edit, or delete an item, click on the item
13:number in the list and follow the prompts.</FONT></P>
14:<P ALIGN=CENTER><FONT SIZE=2>NOTE: This page must be accessed from the Internet
or information will not get stored in the cookie. </FONT></
P>
15:<CENTER>
16:<TABLE CELLPADDING=0 CELLSPACING=5 WIDTH=100%>
17:<SCRIPT LANGUAGE="VBSCRIPT"><!--
18:Call docCreate
19:
20:Sub setVariable(sVariableName, varVariableValue)
21:Document.Cookie = sVariableName & "=" & varVariableValue & "; expires=Monday,
28-Sep-98 12:00:00 GMT"
22: end sub
23:
24:Function readVariable(sVariableName)
25:Dim iLocation
26:Dim iNameLength
27:Dim iValueLength
28:Dim iNextSemicolon
29:Dim sTemp
30:
31:iNameLength = Len(sVariableName)
32:iLocation = InStr(Document.Cookie, sVariableName)
33:If iLocation = 0 Then
34:readVariable = ""
35:Else
36:sTemp = Right(Document.Cookie, Len(Document.Cookie) - iLocation + 1)
37:If Mid(sTemp, iNameLength + 1, 1) <> "=" Then
38:readVariable = ""
39:Else
40:iNextSemicolon = InStr(sTemp, ";")
41:If iNextSemicolon = 0 Then iNextSemicolon = Len(sTemp) +
1
42:If iNextSemicolon = (iNameLength + 2) Then
43: readVariable = ""
44:Else
45: iValueLength = iNextSemicolon - iNameLength - 2
46: readVariable = Mid(sTemp, iNameLength + 2, iValueLength)
47:End If
48:End If
49:End if
50:end function
52:Sub killVariable(sVariableName)
53:setVariable sVariableName, "NULL;expires=Monday, 01-Jan-95 12:00:00 GMT"
54:end sub
55:
56:Sub addTodo(iRecord)
57:Dim sTodo
58:Dim iChoice
59:
60: iChoice = MsgBox("Do you want to add or delete this item? Click yes to add or
no to delete.", 3, "Add/Delete Item")
61:Select Case iChoice
62:Case 6
63:sTodo = InputBox("What do you want to do?", "Add To-Do")
64:Call setVariable("%%TD" & Trim(iRecord), sTodo)
65:Case 7
66:killVariable "%%TD" & Trim(iRecord)
67:Case 2
68:Exit Sub
69:End Select
70:window.navigate "wwwpim.htm"
71:End Sub
72:
73:Sub addAppt(m,d,y)
74:Dim sAppt
75:Dim TempAppt
76:
77: TempAppt = readVariable("%%AP" & Trim(m) & "/" & Trim(d) & "/" & Trim(y))
78:
79:If Len(Trim(TempAppt)) > 0 Then
80:sAppt = InputBox("Please enter or edit your appointment list for today",
"Add/Edit Appointment", readVariable("%%AP" & Trim
(m)
& "/" & Trim(d) & "/"
& Trim(y)))
81:Else
82:sAppt = InputBox("Please enter or edit your appointment list for today",
"Add/Edit Appointment")
83:End If
84:If Len(Trim(sAppt)) > 0 Then
85:Call setVariable("%%AP" & Trim(m) & "/" & Trim(d) & "/" & Trim(y), sAppt)
86:Else
87:killVariable("%%AP" & Trim(m) & "/" & Trim(d) & "/" & Trim
(y))
88:End If
89:window.navigate "wwwpim.htm"
90:End Sub
91:
92:Sub addAddr(iRecord)
93:Dim Addr
94:Dim iChoice
95:iChoice = MsgBox("Do you want to add or delete this item? Click yes to add or
no to delete.", 3,"Add/Delete Item")
96:
97:Select Case iChoice
98:Case 6
99:Addr=InputBox("What address do you want to add?", "Add Email Address")
100:Call setVariable("%%AD" & Trim(iRecord), Addr)
101:Case 7
102:killVariable "%%AD" & Trim(iRecord)
103:Case 2
104:Exit Sub
105:End Select
106:window.navigate "wwwpim.htm"
107:End Sub
108:
109:Sub ApptStat(m,d,y)
110:Dim sTemp
111: |
|
112: sTemp |
= CStr(d & " " & readVariable("%%AP" & m & "/" & d & |
"/" & y)) |
|
113: If Len(Trim(sTemp)) > 0 AND readVariable("%%AP" & m & "/" & d & "/" & y) <>
"NULL" Then
114: window.status = sTemp
115: Else
116:window.status = CStr(d)
117:End If
118:End Sub
120:Function makTD_URL(iRecord)
121:If Trim(readVariable("%%TD" & Trim(iRecord))) <> "NULL" Then
122: makTD_URL = "<A LANGUAGE=VBScript HREF=#null onClick=addTodo (" &
Trim(iRecord) & ")>" & iRecord & ". " & readVariable("%%TD" &
Trim(iRecord)) & "</A>"
123:Else
124:makTD_URL = "<A LANGUAGE=VBScript HREF=#null onClick=addTodo
(" &
Trim(iRecord) & ")>" & iRecord & ". </A>"
125:End If
126:End Function
128:Function makAD_URL(iRecord)
129:If Trim(readVariable("%%AD" & Trim(iRecord))) <> "NULL" Then
130: makAD_URL = "<A LANGUAGE=VBScript HREF=#null onClick=addAddr (" &
Trim(iRecord) & ")>" & iRecord & ". " & "</a>" &
"<A HREF=mailto:" &
readVariable("%%AD" & Trim(iRecord)) & ">" & readVariable("%%AD" &
Trim(iRecord)) & "</A>"
131:Else
132:makAD_URL = "<A LANGUAGE=VBScript HREF=#null onClick=addAddr
(" &
Trim(iRecord) & ")>" & iRecord & ". " & "</A>"
133: End If
134: End Function