Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Creating A Procedure

Visual Basic automates much of the work of creating a procedure. Let’s walk through the steps of creating the DisplayResult procedure for the Calculator project.

1.  Load the Calculator project, then open the Code Editing window (click on the View Code button in the Project Explorer window).
2.  Select Add Procedure from the Tools menu. Visual Basic displays the Add Procedure dialog box, shown in Figure 5.5.
3.  Enter “DisplayResult” in the Name box. Procedure names follow the same rules as variable names—of course, the name should describe what the procedure does.
4.  Be sure that the Sub option is selected in the Type section of the dialog box.
5.  Click on OK.

Visual Basic opens a new editing window with the skeleton of the procedure entered in it, as shown here.

Public Sub DisplayResult( )

End Sub


TIP:  Public Vs. Private

There are two possibilities when declaring a procedure. The Public and Private keywords determine whether a procedure can be called only by code in its own module (Private) or can be called from other modules in the application (Public). Procedures are Public by default, but Visual Basic sticks the keyword on them anyway. Use of these keywords is related to the concept of scope, which is discussed later in this chapter.



Figure 5.5  The Add Procedure dialog box.

The next step is to specify the procedure arguments. An argument is the way a program passes information to a procedure. A procedure can have no arguments, or as many arguments as are needed. Arguments can be any Basic data type. We will need only one for this procedure: a numeric argument that passes the number to be displayed by the calculator. Each procedure argument has a name and a type, similar to a Dim statement. Edit the first line of the procedure as follows:

Public Sub DisplayResult(X As Double)

We are specifying that the procedure DisplayResult takes one type Double argument named X. If we needed to specify additional arguments, they would be separated by commas.

The procedure’s main job is to display a numeric value with proper formatting. How do we want the number to be displayed? On most hand-held calculators, during entry the number is displayed exactly as it is entered. We’ve seen how this is done in the event procedure for the number buttons. Once the Enter key is pressed, however, the number is displayed with full accuracy. For example, if you press 2.5 (but not Enter), the display reads “2.5”. Once you press Enter, it reads “2.500000000”. This enables the user to tell whether he or she is in the middle of an entry or whether the display is showing a calculated or entered number.

We also have to deal with very large and very small numbers. Our calculator display will show approximately 16 digits, with the exact capacity depending on the size of the display Text Box and the font. For very large and very small numbers, however, the use of scientific notation greatly improves readability. This method expresses numbers as a value between 1 and 10, multiplied by 10 raised to a specified power. For example, a small number, such as 120, would be expressed as 1.2 multiplied by 102 (or, in computer notation, 1.2E + 002). A large number, such as 1,234,756,000,000,000,000,000, would be expressed as 1.234756E + 021.

What we’ll do, then, is test the number that is to be displayed, formatting it according to its value:

  If it’s 0, simply display 0.00.
  If it’s not too big (greater than 999,999,999) or too small (less than 0.000000001), we will display 10 decimal places, with commas separating the thousands.
  If it’s outside these boundaries, we will display it in scientific notation, using six decimal places.

To implement this display scheme, we will turn to two of Visual Basic’s built-in functions. These functions are identical to user-defined functions, except they are part of Visual Basic rather than being written by the programmer. Well over 100 built-in functions are available to perform a variety of commonly needed tasks, such as text handling, trigonometric calculations, date and time manipulation, user input, and error handling. I can’t cover all of these functions, but I will explain quite a few as we encounter them. You can view a complete function listing in Visual Basic’s online Help: Select Contents from the Help menu, then select Visual Basic Documentation|Reference|Functions to view an alphabetical listing.

The first function we will use is Abs, which returns the absolute value of its argument. This means that negative numbers are converted to positive, and positive numbers are not changed. Thus, Abs(8) returns 8, and Abs(-10) returns 10.

The other function we’ll be using is Format, which takes two arguments: a number and a format specifier. The format specifier is a string containing special characters that indicate how the number is to be formatted for display. The return value is a string containing the formatted number. For full details on how to construct format specifiers, refer to Visual Basic’s Help system. For our purposes, we simply need to know the following:

  A # character specifies a digit position that will be used if needed and omitted if not needed.
  A 0 character specifies a digit position that will be displayed whether needed or not, being filled with a 0 as required.
  The E+ characters specify scientific notation.

Now you can see the purpose of the two constants that we declared in the declarations section of the form module. The first one

Const DISPLAY_FORMAT1 = “###,###,###,###.0000000000”

will format a number with up to 12 digits to the left of the decimal point, with thousands separators, and 10 digits to the right of the decimal point padded with 0s if needed. For example, the number 4098.998 would be formatted as 4,098.9980000000. We will use this format specifier for numbers that are not too big or too small, falling within the range we specified earlier. The second format specifier

Const DISPLAY_FORMAT2 = “#.######E+000”

will format a number in scientific notation, with up to six decimal places and a three-digit exponent.

Now we have the tools needed to create the DisplayResult procedure. Finish editing the procedure, as shown in Listing 5.3.

Listing 5.3 The DisplayResult procedure.

Public Sub DisplayResult(X As Double)

‘ Display the value properly formatted.

If X = 0 Then
    txtDisplay.Text = “0.00”
ElseIf (Abs(X) < 999999999 And Abs(X) > 0.000000001) Then
    txtDisplay.Text = Format(X, DISPLAY_FORMAT1)
Else
    txtDisplay.Text = Format(X, DISPLAY_FORMAT2)
End If

NewEntry = True

End Sub


TIP:  Visual Basic Helps You Out

I’m sure you have noticed that Visual Basic is keeping an eye on you as you enter code. Whenever you complete a line of code and move the cursor away, Visual Basic reviews the line for certain syntax errors. For example, if the line contains an unmatched parenthesis, Visual Basic will catch it. When an error is detected, Visual Basic pops up a dialog box with a description of the error, highlights the line of code, and positions the cursor where it thinks the error might be. You can either close the dialog box and fix the error or wait until you run the program. Certain kinds of syntax errors are caught only when the program runs (in the Visual Basic development environment—not as a standalone). Again, Visual Basic highlights the line with the error and displays a dialog box. After fixing the error, press F5 to restart the program.

Some programmers find the automatic syntax checking to be annoying. To turn it off, select Options from the Tools menu to display the Options dialog box. Click on the Editor tab, and turn off the Auto Syntax Check option. This way, errors will be caught only when the program runs.


Programming The Enter Button

Our next task is to write the event procedure for the Enter button. You’ll see that it is relatively simple, because we already put much of the needed functionality in the DisplayResult procedure. All that’s required is to set the NewEntry and DisplayHasData flags to True and then call DisplayResult. Double-click on the Enter button to display its Click event procedure, then add the code shown in Listing 5.4.

Listing 5.4 The Enter button’s Click event procedure.

Private Sub cmdEnter_Click()

‘ Set NewEntry to True as we are finished
‘ with this entry.
‘
‘ Set DisplayHasData to True.
‘
‘ Call DisplayResults to put the number
‘ in the display in proper format.

NewEntry = True
DisplayHasData = True
DisplayResult (txtDisplay.Text)

End Sub

Programming The +/-, Clear, Decimal, And Backspace Buttons

The remaining buttons are also fairly easy to program. Remember, double-click on the button to display its Click event procedure. You can also select the control and the event from the lists that are displayed at the top of the Code Editing window.


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.