![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
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, 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 ProcedureBefore 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:
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.
|
![]() |
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. |