Visual Basic Tutorial tip (step by step)
As you learned in Lesson 1, the Microsoft Visual Basic programming environment contains several powerful tools to help you run and manage your programs. Visual Basic also contains everything you need to build your own applications for Windows from the ground up. In this lesson, you’ll learn how to create a simple but attractive user interface with the controls in the Visual Basic toolbox. Next, you’ll learn how to customize the operation of these controls with special characteristics called property settings. Then, you’ll see how to identify just what your program should do with text-based program code. Finally, you’ll learn how to save and run your new program (a Las Vegas–style slot machine) and how to compile it as an executable file.

Lucky Seven: Your First Visual Basic Program

The Windows-based application you’re going to construct is Lucky Seven, a game program that simulates a lucky number slot machine. Lucky Seven has a simple user interface and can be created and compiled in just a few minutes using Visual Basic. (If you’d like to run a completed version of Lucky.exe before you start, you can find it in the Vb6SbsLess02 folder on your hard disk.) Here’s what your program will look like when it’s finished:



Programming Steps

The Lucky Seven user interface contains two command buttons, three lucky number windows, a graphic of a stack of coins, and the label Lucky Seven. These elements were produced in the program by creating seven objects on the Lucky Seven form and then changing several properties for each object. After the interface was designed, program code for the Spin and End command buttons was added to the program to process the user’s button clicks and produce the random numbers. To re-create Lucky Seven, you’ll follow three essential programming steps in Visual Basic: creating the user interface, setting the properties, and writing the program code. The process for Lucky Seven is summarized in the table on the following page.



















Programming stepNumber of items
1. Create the user interface.7 objects
2. Set the properties.10 properties
3. Write the program code.2 objects

Another way to think about the Lucky Seven program is to use the following algorithm, or list of programming steps. Creating an algorithm can be a useful starting point when developing a program.



Creating the User Interface

In this exercise you’ll start building Lucky Seven by creating a new project and then using controls in the toolbox to construct the user interface.

Create the user interface

 

  1. On the File menu, click the New Project command.


You start a new programming project by clicking the New Project command on the File menu.

Click No if you are asked whether you want to save any changes to the StepUp program from Lesson 1. This removes the StepUp program from memory.



  1. Click OK to create a standard 32-bit Visual Basic application.


Visual Basic cleans the slate for a new programming project and displays in the center of the screen a blank form you can use to build your user interface.

Now you’ll enlarge this form, and then you’ll create the two buttons in the interface.



  1. Position the mouse pointer over the lower-right corner of the Form window (not the Project Container window) until the mouse changes into a sizing pointer, and then increase the size of the form to make room for the objects in your program.


As you resize the form, scroll bars appear in the Project window, as shown in the following illustration:



To see the entire form without obstruction, resize the Project Container window to remove the scroll bars and move or close the Properties window, the Project window, and the Form Layout window.

Now you’ll practice a command button object on the form.

tip

Confused by all this resizing? Don’t sweat it. You’re working with two windows here: the Project Container window and the Form window (which fits inside the Project Container window). The troublesome part is that you resize each window by using the resizing pointer in the lower-right corner of the frame, and these two corners tend to overlap a bit. To get more real estate to design your projects, you may want to run Windows in 800 x 600 mode, which you can do by clicking the Windows desktop with the right mouse button, clicking Properties, clicking the Settings tab, and then moving the Desktop Area slider to 800 x 600.

  1. Click the CommandButton control in the toolbox, and then place the mouse pointer over the form.




CommandButton control

The CommandButton control is selected, and the mouse pointer changes to crosshairs when it rests on the form. The crosshairs are designed to help you draw the rectangular shape of a command button. When you hold down the left mouse button and drag, the command button object takes shape and snaps to the grid formed by the intersection of dots on the form.

Try creating your first command button now.



  1. Move the mouse pointer close to the upper-left corner of the form, hold down the left mouse button, and then drag down and to the right. Stop dragging and release the mouse button when you have a command button similar to the one shown here:




The name of the command button is Command1.

A command button with selection handles appears on the form. The button is named Command1, the first command button in the program. (You might make a mental note of this button name—you’ll see it again later when you write your program code.)

You can move command buttons by dragging them with the mouse and you can resize them by using the selection handles whenever Visual Basic is in design mode (whenever the Visual Basic programming environment is active). When a program is running, however, the user will not be able to move interface elements unless you have changed a special property in the program to allow this. You’ll practice moving and resizing the command button now.

Move and resize a command button



  1. Drag the command button to the right by using the mouse.


The command button snaps to the grid when you release the mouse button. The form grid is designed to help you edit and align different user interface elements. You can change the size of the grid by clicking the Options command on the Tools menu and then clicking the General tab.



  1. Position the mouse pointer on the lower-right corner of the command button.


The grid helps you design your user interface.

When the mouse pointer rests on a corner or a side of a selected object, it changes into a sizing pointer. You can use the sizing pointer to change the shape of an object.



  1. Enlarge the object by holding down the left mouse button and dragging the pointer down and to the right.


When you release the mouse button, the command button changes size and snaps to the grid.



  1. Use the sizing pointer to return the command button to its original size, and then move the button back to its original location on the form.


Now you’ll add a second command button to the form, below the first button.

Add a second command button



  1. Click the CommandButton control in the toolbox.




CommandButton control



  1. Draw a command button below the first button on the form. (For consistency, create a command button of the same size.)



  1. Move or resize the button as necessary after you place it. If you make a mistake, feel free to delete the command button and start over.


You can delete an object by selecting the object on the form and then pressing Del.

Add the number labels

Now add the labels used to display the numbers in the program. A label is a special user interface element designed to display text, numbers, or symbols when a program runs. When the user clicks the Lucky Seven program’s Spin button, three random numbers appear in the label boxes. If one of the numbers is a seven, the user hits the jackpot.

  1. Click the Label control in the toolbox, and then place the mouse pointer over the form.


Label control

The Label control is selected, and the mouse pointer changes to crosshairs when it rests on the form.



  1. Create a small rectangular box like the one shown below.


The label object you have created is called Label1, the first label in the program. Now you’ll create two more labels, named Label2 and Label3, on the form.





  1. Click the Label control, and then draw a label box to the right of the first label.


Make this label the same size as the first. The caption "Label2" will appear in the label.



  1. Click the Label control again and add a third label to the form, to the right of the second label.


The caption "Label3" will appear in the label.

tip

As you create labels in this exercise, take a look at the pop-up box that appears next to the labels as you draw them. This box, which contains horizontal and vertical measurements, is called a sizing box. The numbers give the horizontal and vertical dimensions, respectively, of the object you are creating. The numbers are in units of measure called twips; a twip is one-twentieth of a point. (A point is 1/72 inch, so a twip is 1/1440 inch.) You can use the sizing box to compare the relative sizes of objects you’re creating. After you create the object, the same information is also displayed on the right side of the toolbar.

Now you’ll use the Label control to add a descriptive label to your form. This will be the fourth and final label in the program.

  1. Click the Label control in the toolbox.



  1. Create a larger rectangle directly below the two command buttons.


When you’ve finished, your four labels should look like those in the following illustration. (You can resize the label objects if they don’t look quite right.)



Now you’ll add an image box to the form to display the stack of coins the user wins when he or she draws a seven and hits the jackpot. An image box is designed to display bitmaps, icons, and other artwork in a program. One of the best uses for an image box is to display a piece of Visual Basic clip art.

Add an image



  1. Click the Image control in the toolbox.


Image control



  1. Using the Image control, create a large rectangular box directly beneath the three number labels.


When you’ve finished, your image box object should look like the following:



This object will be called Image1 in your program; you’ll use this name later in the program code.

Now you’re ready to customize your interface by setting a few properties.

Setting the Properties

As you discovered in Lesson 1, you can change properties by selecting objects on the form and changing their settings in the Properties window. You’ll start setting the properties in this program by changing the caption settings for the two command buttons.

Set the command button properties



  1. Click the first command button (Command1) on the form.


The command button is surrounded by selection handles.



  1. Double-click the Properties window title bar.


The Properties window is enlarged to full size, as shown in the illustration on the following page.

 

 



The Properties window lists the settings for the first command button. These include settings for the background color, caption, font height, and width of the command button.

  1. Double-click the Caption property in the left column of the Properties window.


The current Caption setting ("Command1") is highlighted in the Properties window.



  1. Type Spin and press Enter.


The Caption property changes to "Spin" in the Properties window and on the form. Now change the caption of the second button to "End". (You’ll select the second button in a new way this time.)



  1. Open the object drop-down list box at the top of the Properties window.


A list of the interface objects in your program appears in the list box:





  1. Click Command2 (the second command button) in the list box.


The property settings for the second command button appear in the Properties window.



  1. Double-click the current Caption property ("Command2"), type End, and then press Enter.


The caption of the second command button changes to "End".

tip

Using the object drop-down list box is a handy way to switch between objects in your program. You can also switch between objects on the form by clicking each object.

Now you’ll set the properties for the labels in the program. The first three labels will hold the random numbers generated by the program and will have identical property settings. (You’ll set most of them as a group.) The descriptive label settings will be slightly different.

Set the number label properties



  1. Click the first number label, and then, holding down the Shift key, click the second and third number labels. (If the Properties window is in the way, move it to a new place.)


To select more than one object on a form, hold down the Shift key while clicking the objects.

A set of selection rectangles appears around each label that you click. When you’ve selected all three labels, release the Shift key.

tip

Because more than one object is selected, only those properties that can be changed as a group are displayed in the Properties window. You’ll change the Alignment, BorderStyle, and Font properties now so that the numbers that appear in the labels will be centered, boxed, and identical in font and point size.



  1. Click the Alignment property, and then click the drop-down list box arrow that appears to the right.


A list of alignment options appears in the list box.



  1. Click the 2 - Center option.


The Alignment property for each of the selected labels changes to 2 - Center.

Now you’ll change the BorderStyle property.



  1. Click the BorderStyle property, and then click the drop-down list box arrow that appears to the right.


A list of the valid property settings (0 - None and 1 - Fixed Single) appears in the list box.



  1. Click 1 - Fixed Single in the list box to add a thin border around each label.


Now you’ll change the font for the labels by changing settings for the Font property.



  1. Double-click the Font property in the Properties window.


The Font dialog box appears, as shown here:





  1. Change the font to Times New Roman, the font style to Bold, and the point size to 24, and then click OK.


The label captions appear in the font, style, and size you specified.

Now you’ll delete the three captions so that the boxes will be empty when the program starts. (Your font selections will remain with the labels because they are stored as separate properties.) To complete this operation, you’ll first need to select each of the labels individually.



  1. Click the form to remove the selection handles from the three labels, and then click the first label.



  1. Double-click the Caption property, and then press Del.


The caption of the Label1 object is deleted. You’ll use program code to put a random "slot machine" number in this property later in this lesson.



  1. Delete the captions in the second and third labels on the form.


You’ve finished with the first three labels. Now you’ll change the Caption, Font, and ForeColor properties of the last label.

Set the descriptive label properties



  1. Click the fourth label object on the form.



  1. Change the Caption property to "Lucky Seven".



  1. Double-click the Font property, and use the Font dialog box to change the font to Arial, the font style to Bold, and the point size to 20. Click OK.


The font in the label box is updated. Notice that the text in the box wrapped to two lines because it no longer fits on one. This is an important concept: the contents of an object must fit inside the object. If they don’t, the contents will wrap or be truncated.

Now you’ll change the foreground color of the text.



  1. Double-click the ForeColor property in the Properties window.


A System tab and a Palette tab appear in a list box, providing you with two options to change the color of your object. The System tab displays the current colors used for user interface elements in your system. (The list reflects the current settings on the Appearance tab in your desktop’s property sheet.) The Palette tab contains all the available colors in your system.



  1. Click the Palette tab, and then click the box containing dark purple.


The text in the label box changes to dark purple. The selected color is translated into a hexadecimal (base 16) number in the Properties window. Most programmers won’t have to deal with this format often, but it is interesting to see how Visual Basic actually records such information inside the program.

Now you’re ready to set the properties for the last object.

The Image Box Properties

The image box object will contain the graphic of the stack of coins in your program. This graphic will appear when the user hits the jackpot (that is, when at least one seven appears in the number labels). You need to set the Stretch property to accurately size the graphic; the Picture property, which specifies the name of the graphics file to be loaded into the image box; and the Visible property, which specifies the picture state at the beginning of the program.

Set the image box properties



  1. Click the image box object on the form.



  1. Click the Stretch property in the Properties window, click the drop-down list box arrow, and then click True.


Setting Stretch to True before you open a graphic will make Visual Basic resize the graphic to the exact dimensions of the image box. (Typically, you set this property before you set the Picture property.)



  1. Double-click the Picture property in the Properties window.


The Load Picture dialog box appears, as shown in the following illustration:





  1. Navigate to the Vb6Sbs folder in the Load Picture dialog box.


The subfolders in the Vb6Sbs folder appear.



  1. Double-click the Less02 folder.


The Windows metafile Coins.wmf appears in the Load Picture dialog box. Windows metafiles contain graphics objects that can be rendered in a variety of different sizes, so they look good in small and large boxes.



  1. Select the file Coins.wmf in the dialog box, and then click Open.


The Coins Windows metafile is loaded into the image box on the form.

Now you’ll change the Visible property to False so that the coins will be invisible when the program starts. (You’ll make them appear later with program code.)



  1. Click the Visible property. Click the Visible drop-down list box arrow.


The valid settings for the Visible property appear in a list box.



  1. Click False to make the image invisible when the program starts.


The Visible property is set to False. This affects the image box when the program runs, but not now while you are designing it. Your completed form looks like this:





  1. Double-click the Properties window’s title bar to return it to the docked position.


Writing the Code

Now you’re ready to write the code for the Lucky Seven program. Because most of the objects you’ve created already "know" how to work when the program runs, they’re ready to receive input from the user and process it automatically. The inherent functionality of objects is one of the great strengths of Visual Basic—once objects are placed on a form and their properties are set, they’re ready to run without any additional programming. However, the "meat" of the Lucky Seven game—the code that actually calculates random numbers, displays them in boxes, and detects a jackpot—is still missing from the program. This computing logic can be built into the application only by using program statements—code that clearly spells out what the program should do each step of the way. Because the program is driven by the Spin and End buttons, you’ll associate the code for the game with those buttons. The Code window is a special window in the programming environment that you use to enter and edit Visual Basic program statements.

Reading Properties in Tables

In this lesson, you’ve set the properties for the Lucky Seven program step by step. In future lessons, the instructions to set properties will be presented in table format unless a setting is especially tricky. Here are the properties you’ve set so far in the Lucky Seven program in table format, as they’d look later in the book.

































ObjectPropertySetting
Command1Caption"Spin"
Command2Caption"End"
Label1, Label2, Label3BorderStyle

Alignment

Font

Caption
1 – Fixed Single

2 – Center

Times New Roman, Bold, 24-point

(Empty)
Label4Caption

Font

ForeColor
"Lucky Seven"

Arial, Bold, 20-point

Dark purple (&H008000808)
Image1Picture

Stretch

Visible
"Vb6SbsLess02coins.wmf"

True

False

In the following steps, you’ll enter the program code for Lucky Seven in the Code window.

Use the Code window



  1. Double-click the End command button on the form.


The Code window appears, as follows:



If the window is smaller than the one shown above, resize it with the mouse. (The exact size is not that important because the Code window includes scroll bars that you can use to examine long program statements.)

Inside the Code window are program statements that mark the beginning and the end of this particular Visual Basic subroutine, or event procedure, a block of code associated with a particular object in the interface:

Private Sub Command2_Click()

End Sub

The body of a procedure always fits between these lines and is executed whenever a user activates the interface element associated with the procedure. In this case, the event is a mouse click, but as you’ll see later in the book, it could also be an event of a different type.



  1. Type End, and press the Down arrow key.


As you type the statement, the letters appear in black type in the Code window. When you press the Down arrow key (you could also press Enter or simply click a different line), the program statement turns blue, indicating that Visual Basic recognizes it as a valid statement, or keyword, in the program.

You use the program statement End to stop your program and remove it from the screen. The Visual Basic programming system contains several hundred unique keywords such as this, complete with their associated operators and symbols. The spelling of and spacing between these items are critical to writing program code that will be accurately recognized by the Visual Basic compiler.

The End statement stops the execution of a program.

tip

Another name for the exact spelling, order, and spacing of keywords in a program is statement syntax.



  1. Move the cursor to the beginning of the line with the End statement in it, and press the Spacebar four times.


The indent moves the End statement four spaces to the right to set the statement apart from the Private Sub and End Sub statements. This indenting scheme is one of the programming conventions you’ll use throughout this book to keep your programs clear and readable. The group of conventions regarding how program code is organized in a program is often referred to as program style.

Now that you’ve written the code associated with the End button, you’ll write code for the Spin button. These programming statements will be a little more extensive and will give you a chance to learn more about program syntax and style. You’ll study each of the program statements later in the book, so you don’t need to know everything about them now. Just focus on the general structure of the program code and on typing the program statements exactly as they are printed. (Visual Basic is fussy about spelling and the order in which keywords and operators appear.)

Write code for the Spin button



  1. Open the Object drop-down list box in the Code window.


The Lucky Seven interface objects appear in the list box, as shown below:



  1. Click Command1 in the list box.


A procedure associated with the Command1 button appears above the first procedure.

By default, Visual Basic displays all the event procedures for a form in one window, so you can easily switch back and forth between them. (A horizontal line appears between the procedures so you can keep them apart.) Alternatively, you can view one procedure per window by clicking the tiny Procedure View button in the bottom-left corner of the Code window. To see all the procedures again in one window, click the Full Module View button located just to the right of the Procedure View button.

Although you changed the caption of this button to "Spin", its name in the program is still Command1. (The name and the caption of an interface element can be different to suit the needs of the programmer.) Each object can have several procedures associated with it, one for each event it recognizes. The click event is the one we’re interested in now because users will click the Spin and End buttons when they operate the program.



  1. Type the program lines shown on the following page between the Private Sub and End Sub statements, pressing Enter after each line and taking care to type the program statements exactly as they appear here. (The Code window will scroll to the left as you enter the longer lines.) If you make a mistake (usually identified by red type), delete the incorrect statements and try again.


tip

As you enter the program code, Visual Basic formats the text and displays different parts of the program in color to help you identify the various elements. When you begin to type a property, Visual Basic also displays the available properties for the object you’re using in a list box, so you can double-click the property or keep typing to enter it yourself. If Visual Basic displays an error message, you may have misspelled a program statement. Check the line against the text in this book, make the necessary correction, and continue typing. (You can also delete a line and type it from scratch.) Readers of previous editions of this book have found this first typing exercise to be the toughest part of the lesson—"But Mr. Halvorson, I know I typed it just as written!"—so please give this program code your closest attention. I promise you, it works!

 

Image1.Visible = False ‘ hide coins

Label1.Caption = Int(Rnd * 10) ‘ pick numbers

Label2.Caption = Int(Rnd * 10)

Label3.Caption = Int(Rnd * 10)

‘if any caption is 7 display coin stack and beep

If (Label1.Caption = 7) Or (Label2.Caption = 7) _

Or (Label3.Caption = 7) Then

Image1.Visible = True

Beep

End If

When you’ve finished, the Code window should look like the following:



A Look at the Command1_Click Procedure

The Command1_Click procedure is executed when the user clicks the Spin button on the form. The procedure uses some pretty complicated statements, and because I haven’t formally introduced them yet, the whole thing may look a little confusing. However, if you take a closer look you’ll probably see a few things that look familiar. Taking a peek at the contents of the procedures will give you a feel for the type of program code you’ll be creating later in this book. (If you’d rather not have a look, feel free to skip to the next section, "Saving the Program.")

The Command1_Click procedure performs three tasks: it hides the coin stack, creates three random numbers for the label windows, and displays the coin stack when the number seven appears. Let’s look at each of these steps individually.

The Command1_Click procedure is the heart of the Lucky Seven program.

The first task in the procedure is accomplished by the line

Image1.Visible = False ‘ hide coins

This line is made up of two parts: a program statement and a comment. The program statement (Image1.Visible = False) sets the Visible property of the first image box object (Image1) to False (one of two possible settings). You might remember that you set this property to False once before by using the Properties window. You’re doing it again now in the program code because the first task is a spin and you need to clear away any coins that might have been displayed in a previous game. Because the property will be changed at runtime and not at design time, the property needs to be set by using program code. This is a handy feature of Visual Basic, and we’ll talk about it more in Lesson 3.

The second part of the first line (the part displayed in green type on your screen) is called a comment. Comments are explanatory notes included in program code following a single quotation mark (). Programmers use comments to describe how important statements work in a program. These notes aren’t processed by Visual Basic when the program runs; they exist only to document what the program does. You’ll want to use comments often when you write Visual Basic programs to leave a "plain English" record of what you’re doing.

Comments describe what program statements do.

The next three lines handle the random number computations. The Rnd function in each line creates a random number between 0 and 1 (a number with a decimal point), and the Int function multiplies the numbers by 10 and rounds them to the nearest decimal place. This computation creates random numbers between 0 and 9 in the program. The numbers are then assigned to the Caption properties of the first three labels on the form, and the assignment causes the numbers to be displayed in boldface, 24-point, Times New Roman type in the three label windows.

G02X01

 

 

The last group of statements in the program checks whether any of the random numbers is seven. If one or more of them is, the stack of coins is made visible on the form and a beep announces a jackpot. Each time the user clicks the Spin button, the Command1_Click procedure is called and the program statements in the procedure are executed.

Saving the Program

Now that you’ve completed the Lucky Seven program, you should save it to disk. Visual Basic saves your form’s code and objects in one file and the "packing list" of project components in another file. (The project components are listed in the Project window.) You can use these component files individually in other programming projects by using the Add File command on the Project menu. To save a program in Visual Basic, click Save Project As on the File menu or click the Save Project button on the toolbar.

Save the Lucky Seven program



  1. On the File menu, click the Save Project As command.


You can save your program at any time during the programming process.

The Save File As dialog box appears, prompting you for the name and storage location for your form.



  1. Select the Less02 folder in the dialog box, if it’s not already selected.


You’ll save your project files in the practice folder that the Microsoft Visual Basic 6 Step by Step Practice Files installation program created on your hard disk. (You can specify a different folder if you like.)



  1. Type MyLucky in the File Name text box, and press Enter.


important

I recommend that you save each project you create in this book by using the My prefix to keep a record of your progress and to preserve the original practice files. That way you can check the original files if you have any problems.

The Lucky Seven form is saved under the name MyLucky.frm. The Save Project As dialog box then appears:





  1. Type MyLucky and press Enter.


The Lucky Seven project is saved under the name MyLucky.vbp. To load this project again later, click the Open Project command on the File menu and click MyLucky in the Open Project dialog box. You can also load a recently used project by clicking the project name at the bottom of the Visual Basic File menu.

Congratulations! You’re ready to run your first real program. To run a Visual Basic program from the programming environment, you can click Start on the Run menu, click the Start button on the toolbar, or press F5. Try running your Lucky Seven program now. If Visual Basic displays an error message, you may still have a typing mistake or two in your program code. Try to fix it by comparing the printed version in this book with the one you typed, or load Lucky from your hard disk and run it.

The complete Lucky program is located in the Vb6SbsLess02 folder.

Run the program



  1. Click the Start button on the toolbar.


Start button

The Lucky Seven program runs in the programming environment. The user interface appears, just as you designed it.

  1. Click the Spin button.


The program picks three random numbers and displays them in the labels on the form, as follows:

>>

Because a seven appears in the first label box, the stack of coins appears and the computer beeps. (The sound depends on your Windows Control Panel setting.) You win!



  1. Click the Spin button 15 or 16 more times, watching the results of the spins in the number windows.


About half the time you spin, you hit the jackpot—pretty easy odds. (The actual odds are about 3 times out of 10; you’re just lucky at first.) Later on you might want to make the game tougher by displaying the coins only when two or three sevens appear. (You’ll see how to do this when you learn more about modules and public variables in Lesson 10.)



  1. When you’ve finished experimenting with your new creation, click the End button.


The program stops, and the programming environment reappears on your screen.

Building an Executable File

Your last task in this lesson is to complete the development process and create an application for Windows, or an executable file. Applications for Windows created with Visual Basic have the filename extension .exe and can be run on any system that contains Windows 95, Windows 98, or Windows NT 3.51 or later, and the necessary support files. (Visual Basic installs these support files—including the dynamic link libraries and ActiveX controls—automatically. If you plan to distribute your applications, see Microsoft Visual Basic 6.0 Programmer’s Guide by Microsoft Press for more information.)

An .exe file can run under any recent version of Microsoft Windows.

 

Try creating MyLucky.exe now.

Create an executable file



  1. On the File menu, click the Make MyLucky.exe command. (Visual Basic adds your program’s name to the command automatically.)


The Make Project dialog box appears, as follows:



tip

The Project Properties dialog box (accessed from the Project menu) contains a Compile tab that you can use to control advanced features related to your program’s compilation. These include optimizations for fast, efficient code; small, compact code; debugging; and other specialized operating conditions. By adding these sophisticated features to the compilation process, Visual Basic makes available to the professional developer tools that have traditionally been associated with the most efficient compilers, such as Microsoft Visual C++.

The dialog box contains text boxes and list boxes that you can use to specify the name and location of your executable file on disk. It also contains an Options button that you can click to open the Project Properties dialog box. You can use the Project Properties dialog box to control the program icon and version information associated with the file. By default, Visual Basic suggests the Less02 folder for the location of your exe- cutable file.

  1. Click OK to accept the default filename and location for the file.


Visual Basic creates an executable file on disk in the specified location.

To run this program later under Windows, use the Run command on the Start menu or double-click the filename in Windows Explorer. You can also create a shortcut icon for MyLucky on the Windows desktop by right- clicking the Windows desktop, pointing to New, and then clicking Shortcut. When you are prompted for the location of your application file, click Browse and select the MyLucky executable file in the Vb6SbsLess02 folder. Click the Open, Next, and Finish buttons, and Windows will place an icon on the desktop that you can double-click to run your program. The shortcut icon will look like this:





  1. On the File menu, click Exit to close Visual Basic and the MyLucky project.

  2. The Visual Basic Programming System closes.


Adding to a Program

You can restart Visual Basic at any time and work on a programming project you have stored on disk. You’ll restart Visual Basic now and add a special statement named Randomize to the Lucky Seven program.

Reload Lucky Seven



  1. Click the Start button on the Windows taskbar, point to Programs, point to Visual Basic 6.0 (or Visual Studio), and then click the Visual Basic 6.0 program icon.



  1. Click the Recent tab in the New Project dialog box.


A list of the most recent projects that you have worked on appears in a list box. Because you just finished working with Lucky Seven, MyLucky should be the first project on the list.



  1. Double-click MyLucky to load the Lucky Seven program from disk.


The Lucky Seven program loads from disk, and the MyLucky form appears in a window. (If you don’t see it, click the MyLucky form in the Project window and then click the View Object button.)

Now you’ll add the Randomize statement to the Form_Load procedure, a special procedure that is associated with the form and that is executed each time the program is started.



  1. Double-click the form (not one of the objects) to display the Form_Load procedure.


The Form_Load procedure appears in the Code window, as shown in the following illustration:





  1. Press the Spacebar four times, type Randomize, and press the Down Arrow key.


The Randomize statement is added to the program and will be executed each time the program starts. Randomize uses the system clock to create a truly random starting point, or "seed," for the Rnd statement used in the Command1_Click procedure. You may not have noticed, but without Randomize the Lucky Seven program produces the same string of random spins every time you restart the program. With Randomize in place, the program will spin randomly every time it runs. The numbers won’t follow a recognizable pattern.



  1. Run the new version of Lucky Seven, and then save the project to disk. If you plan to use the new version a lot, you may want to create a new .exe file too. Visual Basic does not update executable files automatically when you change the source code.


If you want to continue to the next lesson



  • Keep Visual Basic running, and turn to Lesson 3.


If you want to exit Visual Basic for now



  • On the File menu, click Exit.


If you see a Save dialog box, click Yes.

Lesson 2 Quick Reference




















































ToDo thisButton
Create a user interfaceUse toolbox controls to place objects on your form, and then set the necessary properties. Resize the form and the objects as appropriate.
Move an objectDrag the object on the form by using the mouse.
Resize an objectClick the object, and then drag the selection handle attached to the part of the object you want to resize.
Delete an objectClick the object, and then press Del.
Open the Code windowDouble-click an object on the form (or the form itself).
or
Click the View Code button in the Project window when a form or module name is highlighted in the Project window.
 

 


Write program codeType Visual Basic program statements associated with the object you want to program in the Code window.
Save a programOn the File menu, click the Save Project As command.
or
Click the Save Project button on the toolbar.
 

 


Create an .exe fileOn the File menu, click the Make filename.exe command.
Reload a projectOn the File menu, click the Open Project command.
or
Double-click the file in the Recent tab of the New Project dialog box.

 

Source    http://www.visualbasicbooks.com

 

 

Reviews:

Post a Comment

Asian Tour Trip © 2014 - Designed by Templateism, Distributed By Blogger Templates | Templatelib

Contact us

Powered by Blogger.