Visual Basic in 12 Easy Lessons vel02.htm

Previous Page TOC Next Page



Lesson 1, Unit 2


First Look at Visual Basic



What You'll Learn


This unit helps you install the Visual Basic programming system that comes with this book. You will find that the installation of Visual Basic is easy because Windows does most of the work for you. If you've installed other Windows programs before, you will have no trouble with installing Visual Basic.



Warning: This book assumes that you have used Windows before. Although you might not be a Windows guru, you should be comfortable with starting Windows, using the mouse, selecting from Windows menus, and so on.



Tip: If you want a good review of Windows fundamentals, get a copy of Alpha Books' The Complete Idiot's Guide to Windows (ISBN 1-56761-175-3) and read it before going further with Visual Basic Programming in 12 Easy Lessons. You'll be glad you did.

Before installing the program, this unit takes a few moments to describe exactly what you get with this book's bundled disk.

The Visual Basic Primer Disk


Concept: With this book, you get a Visual Basic programming system with which you can learn Visual Basic and create your own Windows programs.

The Microsoft Corporation has released several versions of Visual Basic. When version 1.0 hit the market, Visual Basic changed the way in which people viewed Windows programming. Even in its first release, Visual Basic was a powerful programming tool for introductory Windows programmers. With its visual placement of program and screen elements, you will literally draw programs instead of writing them. With every release of Visual Basic, Microsoft keeps adding more functionality and programming power.

This book contains the Visual Basic version 2.0 programming system. This version of Visual Basic is called the Visual Basic Primer Edition. It includes everything in the original—and really expensive—Visual Basic version 2.0 Standard Edition except for these three modifications:



Definition: A compiler creates a stand-alone program.

When you buy a copy of Visual Basic—and pay a whole lot more money than you did for this book and disk!—you get everything you get here plus a compiler. This copy of Visual Basic enables you to create the same programs as the regular copy, but you must run those programs from within the Visual Basic environment. In other words, when you want to run the program that you write, you have to run Visual Basic first and use the Visual Basic menus to load and run your program.

For most beginning and intermediate programmers, the second modification is not much of a limitation. You can add only one form to a program. One form is probably all you would be using anyway. It sometimes takes a fairly complicated Visual Basic program to require a second form.

The third modification would really hinder most people, but not you. This book offers a better tutorial and reference guide than you would ever get with an online reference.

What can you expect with the Visual Basic Primer disk? A version of Visual Basic that includes all the tools that you need to learn the in’s and out’s of Visual Basic. You will add advanced controls to your Visual Basic programs. You will be write programs that behave just like the major Windows applications you use daily. You will learn what programming is all about by mastering the Visual Basic programming language.

Review: The disk that comes with this book contains the Visual Basic Primer programming system, which includes everything you need to create complete Windows applications.

Installing the Visual Basic Primer Disk


Concept: Install the Visual Basic Primer programming system on your computer's hard disk.

Installing the Visual Basic Primer is simple. The installation described in this unit assumes that you want to install the Visual Basic Primer on your hard disk drive named C and that you are installing from the floppy disk drive named A. Change the drive names accordingly if you want to install to or from different drives.

Follow these steps to install the Visual Basic Primer on your system:

  1. Start Windows.

  2. When the Program Manager appears, select the Program Manager File Run command (point to the word File on the menu bar, click the mouse, and then click Run). Your screen will look something like the one in Figure 2.1.

    Figure 2.1. Getting ready to install Visual Basic.



    Warning: Almost every Program Manager displays different program group icons because almost everybody uses a different configuration of software packages. Therefore, your screen might hold a set of icons completely different from what is shown in Figure 2.1. The dialog box shown in the upper-left-hand corner is what matters.

  3. At the Command Line prompt, type A:\SETUP—either uppercase or lowercase is acceptable—and press Enter. The disk in drive A will spin for a few moments, and the installation program will display an installation message box on the screen telling you that the installation process has begun.
    If you get an Application Execution Error when you type the installation setup command, press Enter and check the Command Line prompt that you typed to make sure that you spelled everything correctly. Make sure that there are no spaces in the command.

  4. When you see the Welcome message box, you know that you're on your way to a successful installation. At the Welcome message box, press Continue to tell the installation program that you are ready to complete the process.



    Definition: Default is the value used if you do not type a different value.

  5. As shown in Figure 2.2, Visual Basic prompts you for the exact location where you want to install the program.
    By default, the Visual Basic Primer program installs itself in a directory named VBPRIMER. VBPRIMER is a good name for the directory, so I suggest that you keep this name.

    Figure 2.2. Visual Basic wants to know where you want the program installed.

  6. Click the Continue button to continue the installation.
    When Visual Basic notices that the VBPRIMER directory does not exist, Visual Basic warns you that the directory does not exist with a message prompt. This is a silly message box because, of course, the directory does not exist! If the directory existed, you would already have Visual Basic Primer installed on your computer. Therefore, when Visual Basic tells you that the directory does not exist, mutter under your breath, "No kidding," and press the Create Directory button.

  7. You will see one more message box that you can ignore. When Visual Basic displays the message box shown in Figure 2.3, press OK and don't worry about what the message box says. The message is a carry-over of the official—read expensive—version 2.0 release of Visual Basic, and this message box does not apply here.
    You need about two megabytes of free disk space for Visual Basic and the applications that you create with this book. Assuming there is enough disk space to install Visual Basic, the installation program proceeds to complete the installation. You will see a percentage gauge increase as the installation progresses.

    Figure 2.3. You can completely ignore this message box.



    Warning: Depending on your installation of Windows, one or two additional message boxes might appear that tell you of particular files that the installation program needs to overwrite. You can click the OK button to accept the information and move on. Also, there is another error message box that might appear that indicates an incorrect number of bytes were counted during installation. As bad as this message sounds, if you click Ignore—one of the few times that you can ignore error message boxes—the installation continues successfully.

  8. During the installation, Visual Basic creates a new program group called Visual Basic 2.0 for the Program Manager's Visual Basic Primer icon. When the installation is complete, you are given the choice to start Visual Basic or to return to Windows. For now, return to Windows so that you can learn the standard startup method.



Definition: A control button appears in the upper-left corner of every window.

When you return to the Windows Program Manager, you will see the Visual Basic 2.0 window left open by the installation program. For now, close the window by double-clicking the control button to return the Program Manager to its regular state. (A state of confusion....)

You have now successfully installed the Visual Basic Primer programming system, but there is one last thing you must do to complete the installation of this book's disk. This book's disk also contains all the book's complete working applications. The applications are in a directory called PGMS.

Assuming that you have installed the Visual Basic Primer as described, follow these steps to copy the programs to the VBPRIMER directory so that you can load the programs easily as you work through them:

  1. Start the Windows File Manager. You will find the File Manager icon in the Main program group.

  2. Click the C: drive to activate C if it is not already the active drive.

  3. You'll see the vbprimer directory file folder in the list of directories in the left panel. Click vbprimer to open the file folder.

  4. Press F8 to open the Copy dialog box. Type A:\PGMS\*.* and press Enter to copy all the working applications to the VBPRIMER directory.

  5. Close the File Manager by double-clicking the control button in the upper-left hand window. You have now successfully copied all the applications to your hard disk along with Visual Basic.

Review: Your computer now has this book's Visual Basic program installed. You are ready to start the program and master the environment.

Starting and Stopping Visual Basic


Concept: To write a Windows program with Visual Basic, you have to start Visual Basic. Before exiting Windows, you must exit Visual Basic.

Your Window's Program Manager might have placed the Visual Basic 2.0 icon virtually anywhere on the screen. Look through your icons in the Program Manager until you find the one labeled Visual Basic 2.0. Double-click the icon; the Visual Basic program group appears with a single icon labeled Visual Basic Primer.

Double-click the Visual Basic Primer icon to start Visual Basic. Figure 2.4 shows the Visual Basic opening screen.

Figure 2.4. The Visual Basic startup screen.

As you can see from Figure 2.4 and probably from your own screen, the underlying Program Manager icons often peek through to the Visual Basic programming area. Go ahead and exit Visual Basic, and you will learn a way to keep those Program Manager icons from coming through.

Visual Basic is consistent with most Windows programs in that its menus and commands are similar in many ways to Excel, Access, and other programs with which you might be familiar. To exit Visual Basic, select the Exit command from the File menu. When you do this, Visual Basic quits and you are back at the Program Manager. If the Visual Basic program group is still open, double-click its control button to close the open window.

If you want to keep the Program Manager program group icons from showing through to the Visual Basic desktop, click the Program Manager's Options Minimize on Use command. Display the Options menu once more to see that there is now a checkmark next to the second option. The checkmark means that the Program manager shrinks to a small icon when you start any Windows program. The next time you start Visual Basic, the Program Manager icons will no longer get in your way.



Tip: Here's a great shortcut that even many advanced Windows people do not know: Open the Visual Basic 2.0 icon from the Windows Program Manager. With the Visual Basic Primer icon still highlighted, select File Properties from the Windows menu bar. Press Alt+S to move the text cursor to the Shortcut Key prompt. Press the letter V so that the Shortcut Key prompt changes to Ctrl+Alt+V. Press Enter or click the OK button to close the Program Item Properties dialog box. From now on, you will not even have to open the Visual Basic 2.0 program group when you want to start Visual Basic. Simply press Ctrl+Alt+V from the Windows Program Manager; Visual Basic will start immediately.



Warning: Always exit Visual Basic before you shut off your computer or you might lose part or all of the program that you are writing.

Review: Unless you add a shortcut keystroke to start Visual Basic, starting Visual Basic requires only that you double-click the Visual Basic Primer icon inside the Visual Basic 2.0 Program Manager group. To exit Visual Basic and return to Windows, you can select File Exit.

The Visual Basic Environment


Concept: Before you learn how to write programs with Visual Basic, you must learn all about the Visual Basic screen. Some people think that the Visual Basic screen is confusing at first. They are wrong. The Visual Basic screen is confusing not only at first but also the whole time you use it! Actually, Visual Basic's screen is more busy than it is confusing. Once you learn how to manage the parts of the screen, however, you will feel much more comfortable using Visual Basic.

Being an effective Visual Basic programmer means knowing how to rearrange the Visual Basic screen when needed. Visual Basic does not really look like every Windows program. For example, Microsoft Word is one of the most powerful and most used Windows programs. When you use Word, you basically work within one gigantic window. You can open a second document window and resize the two windows to add more screen elements, but most of the time you work within a single document window.

In Visual Basic, you work with several open windows most of the time. There are several windows—sometimes called window panes in this book—open, and you will often need information from each of the windows. Therefore, you should get familiarized with the screen and its components early on.

Review: Learn all you can about the Visual Basic environment now so that you can concentrate on the language and controls later.

The Five Windows


Concept: The Visual Basic environment contains several windows with which you will work as you build applications.

Figure 2.5 illustrates the major parts of the Visual Basic screen. You might not understand all the components of the screen just yet, but learn the names of the screens now so that you will move right along later when you begin learning how to program Visual Basic.

Figure 2.5. The elements of the Visual Basic screen.



Warning: Actually, Figure 2.5 does not look exactly like your screen probably will look when you first start Visual Basic. You will soon learn how to rearrange the screen so that the screen can look just like the screen in Figure 2.5. Figure 2.5 looks the way it does so that you can see all the major parts of the screen.

Table 2.1 describes each of the five primary windows of Visual Basic. Although you cannot understand all of the descriptions at this point, try to familiarize yourself with the descriptions so that the windows will not be so foreign later when you learn how to program in Visual Basic.

Table 2.1. The primary windows inside the Visual Basic environment.

Window Description
Form Contains the background for the Windows program that you are writing. You draw and place items on the form that your program's user will eventually see and interact with. If you use a Windows word processor, the form would hold the document that you are editing. Although not every Visual Basic program requires forms, most do because most Visual Basic programs exist to display information for, and retrieve information from, the user.
Toolbox The Toolbox window contains your tools. That might seem obvious, but you need to know that the tools of Visual Basic are more often called controls. The toolbox is where you will find controls that you will place on the Form window as you create a Windows program. For example, when you need to ask the user for text, you will select a text box control from the toolbox and place that text box on the form.
Project A Visual Basic Windows program often contains several different kinds and types of files that all work in unison to form the single running application. The Project window contains the list of all the files used in the current application. Given the common Visual Basic terminology, a Visual Basic application is generally called a project. The Project window contains the contents of the project. The Project window is simply a description of the files but the files all reside separately on the disk.
Properties The Properties window describes every individual element in your application. For example, there is a Properties window for a project's form because the form contains properties such as color and size. As you place controls from the Toolbox window onto the Form window, each of those controls has its own properties. Although any one Visual Basic program might have several elements with properties, there is only one Properties window. When you want to see the properties of a different form or control, you change the Properties window to display another set of properties.
Code Unlike most other programming languages, you do not have to write much code as you develop applications in Visual Basic. The more advanced the application needs to be, the more code you will have to write to tie things together. The visual parts of Visual Basic, however, eliminate much of the code that you would have to write if you were still working in a text-based environment. Although you should not expect to understand anything just yet, Figure 2.6 shows a Code window that contains a fairly complex routine. The code in the Code window is the program's source code, which you learned about in the previous unit. When the user runs the program, Visual Basic and your computer interprets that source code and executes the instructions in the source code.

Figure 2.6. A Code window with lots of code.



Note: Much of the time, the code inside the Code window contains setting and retrieval instructions for form controls. For example, if you need to check whether a user clicked a command button or typed a response, you can use code to check for the click.

As with most windows used inside Windows applications, you can move, resize, and close the five windows. Use the mouse to make working with windows simple.



Definition: Maximize means to increase a window to its largest size.

For example, when you first start Visual Basic, the Form window hides the other windows. Usually, the Form window is the largest window because the Form window is the user's background. You can maximize the Form window by double-clicking the mouse on the Form window's title bar or by clicking the maximize button in the window's upper-right hand corner. Double-click the Form window's title bar now to maximize the window. When you do, there is nothing left on the screen.

Obviously, there is a way to see the menu and the other windows. Press Alt to get the top of the screen back so that you can see the menu bar and toolbar again. Display the Window pull-down menu, and select the Project window to see the Project window. Click the Project window's View Code button to see the Code window.



Tip: There are two other ways to display the Code window. You can select View Code to see the window. You can also press F7 to display the code.

Click anywhere on the white portion of the Form window. Clicking any window activates that window, highlights its title bar, and makes all its commands and menus available. When you activate the Form window, the Project window and Code window hide behind the form, but you can get them back by following the descriptions just offered.

To see the form’s Properties window, press F4 or select Window Properties to see the Properties window. Move the mouse cursor over the Properties window and click and hold the mouse button. You can now move the Properties window by dragging the mouse across the screen. When you let up on the mouse button, Visual Basic anchors the Properties window at that point.

Try resizing the Properties window. Move the mouse cursor to any edge or corner of the Properties window. The mouse changes to a double-pointing arrow. By dragging the mouse, you can resize the window.

When you are ready to close a window, the easiest way is to double-click its control button. However, if you click the control button once, you will see the window’s control menu, such as the one shown in Figure 2.7. You have probably seen the control menu in other Windows work that you have done. If not, you can use the control menu to move, resize, and close the window with your keyboard. Using the mouse as just described, however, is easier than using the control menu. To close the control menu, you can click the control button once again or press the Esc key twice.

Figure 2.7. The control menu.

Review: The five primary windows of Visual Basic supply the locations for controls and work areas that you will use to build Visual Basic applications. The Form window is the most important window for the applications that you write because it is on the Form window where you will draw and place interactive controls for the user to work with. The other windows exist to offer help and tools.

The Top of the Screen


Concept: As you learn more about Visual Basic's environment, you will find that Visual Basic conforms well to the standard look and feel of standard Windows programs. Many Windows programs contain menus and toolbars that work much like Visual Basic's.

The top of the screen contains the menu bar and toolbar. The menu bar contains lists of pull-down menus with which you can manage your Visual Basic program. The toolbar supplies quick push-button commands for common tasks.



Warning: Don't confuse the terms toolbar with toolbox. The toolbar appears under the menu bar and contains buttons with icons on them. The toolbox is what is typically called the Toolbox window where the controls are located that you will eventually place on the form.


The Menu Bar


If you have worked much with other Windows programs, you are already familiar with the File, Edit, View, Window, and Help menu bar commands because they are similar across many Windows applications. Table 2.2 describes all the Visual Basic menu bar commands with which you will work.



Note: The menu bar contains additional menus that pull down just like virtually all Windows applications use. These pull-down menus are sometimes called submenus. The commands on the submenus perform tasks or produce dialog boxes that require extra information from you before Visual Basic can issue the commands.


Table 2.2. The menu bar commands.

Command Description
File The File menu contains all file-related commands with which you can load and save Visual Basic applications. It also provides printing access for printed program descriptions as well as the Exit command that you learned about earlier in this unit.
Edit Programmers often use the commands on the Edit menu for copying, cutting, and pasting text and graphical controls among applications. The Edit commands also help you with the creation of your programs by supplying common search and replace actions.
View The View menu command enables you to control the viewing of your application's Code window, various routines that can appear inside the Code window, as well as the toolbar. By hiding the toolbar, you can gain a little extra screen space. For instance, if you wanted more workspace and did not use the toolbar often, you could hide the toolbar by unselecting View Toolbar (the default is selected so that the toolbar appears). The toolbar will disappear. Selecting View Toolbar once again displays the toolbar.
Run When you complete an application, you can see the results of your work with the Run menu. The Run menu enables you to execute programs, halt the execution, and resume the execution after a halt.
Debug One of the most powerful features of Visual Basic is its debugging capability. With the Debug menu, you can execute a Visual Basic program one statement at a time, looking at data values along the way, and stop the program at any point to analyze what is going on. If a program does not behave the way you think it should, the Debug menu will help you pinpoint the cause of the trouble.
Options You can determine the way in which Visual Basic behaves by modifying values within the Option menu. You can control both environment options—the environment is the Visual Basic atmosphere in which you build programs—and project options that determine how each particular application behaves.
Window With the Window menu, you can display the Project, Properties, and Toolbox windows as well as auxiliary areas in Visual Basic such as Visual Basic's color selection box (with which you can assign colors to various controls you place on forms), the Menu Design dialog box (which you use for adding menus to your Visual Basic applications), and the Debug window (where you can work while debugging the program).
Help When you select from the Help menu with the Primer edition of Visual Basic, you will not get online help as you would in the full version. If you select Help Contents, for example, the Visual Basic Primer displays the window shown in Figure 2.8, which describes the help system if you were to use the regularly-priced version of Visual Basic. (If you display this screen, select File Exit to get rid of it.) Some of the lower commands on the Help menu produce a message box that tells you that no help is available. You can, however, display the About box to see how much memory is available as well as the copyright notice and version of the Visual Basic Primer system.

Figure 2.8. Sorry. You will get no help from the Visual Basic Primer Edition.

The Shortcut Access Keys




Definition: An access keystroke is a shortcut method of issuing orders.

Many of the menu bar's commands also activate when you press an access keystroke. For example, if you display the File pull-down menu, you will see the menu shown in Figure 2.9. You can activate any command on the File menu by displaying the menu and selecting a commend. You can also issue orders for four of the commands by pressing an access keystroke.

Figure 2.9. Access keystrokes make selecting certain commands easier.

Instead of selecting File Add File..., you can press Ctrl+D. Instead of selecting Save File, you can press Ctrl+S. The access keystrokes are available from within Visual Basic even if you do not first display the menu. For example, you can save the active file by pressing Ctrl+S without having to take the time to display the File menu first.



Tip: The Ctrl key works just as how the Shift and Alt keys work. Pressing Ctrl+S means press and hold the Ctrl key, and then press the S key while still holding down Ctrl, and then immediately let up on both.

Not all of the access keystrokes require that you use the Ctrl key. For example, some menu commands on the Edit pull-down menu do not require a second key such as Ctrl. Also, the Edit Find Previous command requires Shift+F3.



Tip: As you will see next, many of the toolbar commands provide the same functionality as many of the menu commands.


The Toolbar: Push Button Swiftness


Figure 2.10 shows the toolbar and describes each button on the toolbar. Many of the toolbar buttons represent menu commands. Instead of issuing a menu command by using the mouse or an access key, you can point to a toolbar button to perform the same task.

Figure 2.10. The toolbar contains quick access to many commands.

As you progress through this book and learn how to use commands that appear on the toolbar, you will be reminded when you can use a toolbar button. Some people prefer not to use the toolbar. They either want more screen space or do not think the icons are that easy to remember—they are not easy to remember. Remember that the View Toolbar command hides the toolbar from view if you do not want to see the toolbar.



Warning: Before you remove the toolbar, be sure that you do not need the measurement indicators that appear to the right of the toolbar. The next section explains what the measurement indicators do.

Notice that not all of the toolbar buttons are dark. Some are grayed out, just as some of the pull-down menu bar commands are grayed out at times. Visual Basic knows that certain commands have to be activated at specific times within the program. If you have not copied text or a control to the clipboard, for example, you cannot use the Edit Paste command.

The Measurement Indicators


As you draw and resize images on the Form window, you will often look to the two measurement indicators that appear to the right of the toolbar for help. The first indicator describes the upper-left corner measurement of a control, and the second indicator describes the size of the control.



Definition: A twip is 1/1440 of an inch.

Each of the measurements appear in twips. For example, Figure 2.11 shows a box placed in the center of the Form window. You know from the measurement indicators that the box's upper-left corner appears exactly 3,000 twips from the left edge of the Form window and exactly 1,560 twips from the top edge of the Form window. Likewise, you know that the box is exactly 1,215 twips wide and 495 twips long.

Figure 2.11. The measurement indicators enable you to size and place controls on the Form window.

By using the measurement indicators, you ensure that all screen elements in your user's application are aligned and properly adjusted for the size that you want.



Note: The Form window's grid of dots that you see in the background helps you align images with one other. The grid is sometimes called a snap to grid because controls that you place on the Form window snap to the nearest grid dot location if you place controls between two grid points. If you want to adjust the distance between grid dots, use the Options Environment command. If you want to turn off the grid so that you can place controls between grid points when you want, you can set the Align To Grid option to No from the same menu location.

Review: You have now seen a complete description of the Visual Basic screen and its environment. Although you do not know how to use all the elements in the environment, you are at least familiar with the environment and will recognize the names of the screen elements when they appear later in this book.

Homework



General Knowledge


  1. True or false: You can run but not compile Windows programs using the Visual Basic Primer disk.

  2. True or false: You must install the Visual Basic Primer application onto your hard disk.

  3. How can you set up a shortcut to run Visual Basic from the Program Manager without first opening the Visual Basic program group?

  4. True or false: If your Program Manager contains a set of program groups different from those shown in this unit, you will not be able to install Visual Basic.

  5. What command do you type to start the installation of this book's Visual Basic?

  6. What does the term default mean?

  7. What is the default name of the directory where the Visual Basic Primer installs itself?

  8. How can you exit Visual Basic?

  9. What happens if you turn off the computer before you exit Visual Basic?

  10. Name Visual Basic's five primary windows.

  11. Which window does Visual Basic use as the application's background?

  12. True or false: There is no difference between the toolbox and the toolbar.

  13. True or false: You can hide the toolbox from view.

  14. What are the access keys used for?

  15. What is the toolbar used for?

  16. Why is Visual Basic's menu bar familiar to most Windows users?

  17. How many access keystrokes are available on the Edit pull-down menu?

  18. Why are some of Visual Basic's menu commands and toolbar buttons grayed out at times?

  19. What do the toolbar's measurement indicators do?

  20. What is a twip?

  21. How does the grid help you align controls on the form?


Extra Credit


Locate each toolbar button's corresponding menu command. In doing so, you will better familiarize yourself with the menu and its contents.

Previous Page Page Top TOC Next Page