![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Creating A Procedure Visual Basic automates much of the work of creating a procedure. Lets walk through the steps of creating the DisplayResult procedure for the Calculator project.
Visual Basic opens a new editing window with the skeleton of the procedure entered in it, as shown here. Public Sub DisplayResult( ) End Sub
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 procedures 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. Weve 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 well do, then, is test the number that is to be displayed, formatting it according to its value:
To implement this display scheme, we will turn to two of Visual Basics 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 cant 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 Basics 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 well 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 Basics Help system. For our purposes, we simply need to know the following:
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
Programming The Enter ButtonOur next task is to write the event procedure for the Enter button. Youll see that it is relatively simple, because we already put much of the needed functionality in the DisplayResult procedure. All thats 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 buttons 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 ButtonsThe 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.
|
![]() |
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. |