Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Visual Design + Basic Code = Visual Basic
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous
Table of Contents
Next
As the code in the parentheses indicates, when this procedure is called in response to a button click, Visual Basic passes an argument named Index to the event procedure. Because the 10 number buttons are all part of a control array, this one Click event procedure is called when any one of them is clicked on. The value of Index (which is the same as the value stored in each buttons Index property) specifies which of the buttons was selected. Because of the way we created the control array, each buttons Index property is the same as the number it represents, making programming a bit easier.
What needs to happen when the user clicks on one of the number buttons? Lets begin by taking a look at whats going on. The calculator could be in one of two states. In one state, the user has already entered part of a number, and the just-clicked number needs to be tacked onto the end of what is already in the display. In the other state, the user has completed making the previous entry or calculation, and the just-clicked number represents the start of a new entry. In this latter case, the number in the display should be put on the stack and erased from the display, then the number that was clicked displayed. We will use the Boolean variable NewEntry, which we declared in the General section as a flag for the calculators condition. This flag will be set to False if the user is continuing an existing entry; it will be set to True if the user is starting a new entry. Therefore, the procedure needs to perform two tasks:
If NewEntry is True, erase the display, display the clicked number, and set NewEntry to False.
If NewEntry is False, add the clicked number to the end of the number already in the display.
If NewEntry is True, do we always want to put the existing number in the display on the stack? Not necessarily. Suppose the user started entering a number then cleared it with the Clear button (to be programmed soon). In that case, there is no reason to put the 0 in the display on the stack when the new entry is started. The same is true if the calculator has just been started. Thus, we will use the DisplayHasData flag to determine whether the existing number in the display should be placed on the stack.
The code for this procedure is shown in Listing 5.2. You can see that its quite simple. You should also be able to see why I insisted that you match each buttons Index property with its caption, which results in the Index argument to the event procedure holding the buttons actual value. In this case, the program needs only to use the concatenation operator to tack Index onto the end of the calculators display.
Listing 5.2 The Click event procedure for the array of number buttons.
Private Sub cmdNumbers_Click(Index As Integer)
If were starting a new entry, clear the text box.
If current display has valid data put it
on the stack.
If NewEntry Then
NewEntry = False
If DisplayHasData Then
StackPointer = StackPointer + 1
Stack(StackPointer) = txtDisplay.Text
End If
txtDisplay.Text =
End If
Put the new digit at the end of the text.
txtDisplay.Text = txtDisplay.Text & Index
End Sub
You can test the project at this point by pressing F5. The calculator will display, and you can click on the number buttons to enter numbers in the display. Nothing else works yet, however, so clearly we have some more code to write.
Before we continue, notice how I wrote the condition for the If statement in this code. We needed to see if the variable NewEntry was True. Rather than writing
If NewEntry = True Then
I wrote the following:
If NewEntry Then
Do you see why these two statements are equivalent? If NewEntry is True, then the expression NewEntry = True evaluates to True. Thus, the two expressions are equivalent.
The Display Procedure
Before moving on to the other button event procedures, we are going to create a procedure that will display results in the calculators display. This will be a general procedure, which is different from an event procedure. Lets take time out from our calculator project to learn about general procedures, because they are an essential part of Visual Basic programming.
General Basic Procedures
As you have learned, an event procedure is a separate, self-contained chunk of code that is automatically executed whenever the associated event occurs. A general procedure is similar, except that it is not linked to an event. Rather, a general procedure (or simply procedure, as Ill refer to it) is executed when code elsewhere in the program calls it. The use of procedures (a technique sometimes referred to as structured programming ) is a central part of Visual Basic programming as well as all other programming languages. Procedures emphasize breaking a program into a number of partially independent tasks, with the code for each task being placed in its own procedure. Each time the program needs the task performed, it calls the procedure. This approach offers a number of significant advantages:
The program is easier to write, because the entire project is broken down into a number of smaller, more easily managed tasks.
The program is easier to debug, because you can localize a problem to a specific section of code.
The program is easier to maintain, because modifications made in one part of the program are less likely to have unwanted side effects in other parts of the program.
Code that is inside a procedure (including event procedures) is called procedure level code. Code that is outside a procedure is called module level code. With a few exceptions, you can put any Basic statement in a procedure; the major exception is that you cannot define one procedure within another. Theres no limit to the amount of code you can put in a procedure, but it is good programming practice to keep procedures to a small or moderate size by splitting complex tasks into several smaller tasks. Whats a moderate size? Theres no strict definition, but in my experience its rare to find a procedure beyond 25 or 30 lines of code that could not be profitably broken into two or more smaller procedures.
There are two types of procedures: A function returns a value to the calling program, whereas a sub procedure does not. Otherwise, the two types are identical. An important feature of procedures is the ability to define procedure arguments, which can be thought of as specialized variables for passing information to the procedure when it is called. We will see how to create and use a sub procedure here. Functions, which Ill cover later, arent much different.
Previous
Table of Contents
Next
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
Wyszukiwarka
Podobne podstrony:
2009 05 04 Rozp MON używanie znaków w SZ RP05 04 Oznakowanie i prowadzenie robot pod ruchem05 04 Oznakowanie i prowadzenie robot pod ruchem2010 05 04ZADANIE C1 2009 05 04TI 00 05 04 T plćw 34 Rozp Min Gosp 07 05 04PM1 05 04TI 01 05 04 T pl(2)TI 02 05 04 T pl(2)ZL3 05 04dx25tg 05 04więcej podobnych podstron