Hour 1

Visual Basic at Work

Welcome to Visual Basic! You possess one of the most powerful and enjoyable Windows development tools available today. Visual Basic really is fun, as you'll see throughout this 24-hour tutorial. In this hour you will become familiar with the big picture of Visual Basic 5.

The highlights of this hour include

Whats Visual Basic About?

Controls are tools on the Toolbox window that you place on a form to interact with the user and control the program flow.

Microsoft Visual Basic 5.0, the latest and greatest incarnation of the old BASIC language, gives you a complete Windows application development system in one package. Visual Basic (or VB, as we often call it) lets you write, edit, and test Windows applications. In addition, VB includes tools you can use to write and compile help files, ActiveX controls, and even Internet applications.

New Term: A program is a set of instructions that make the computer do something such as perform accounting. (The term program is often used synonymously with application.)

Visual Basic is itself a Windows application. You load and execute the VB system just as you do other Windows programs. You will use this running VB program to create other programs. VB is just a tool, albeit an extremely powerful tool, that programmers (people who write programs) use to write, test, and run Windows applications.

New Term: A project is a collection of files you create that comprises your Windows application.

Although programmers often use the terms program and application interchangeably (as will be done throughout this 24-hour course), the term application seems to fit the best when you're describing a Windows program because a Windows program typically consists of several files. These files work together in the form of a project. The project generates the final program that the user loads and runs from Windows by double-clicking an icon or by starting the application with the Windows Start menu.

New Term: An application is a collection of one or more files that compile into an executable program.

The role of programming tools has evolved over the past 45 years along with computer hardware. A programming language today, such as Visual Basic, differs greatly from programming languages of just a few years ago. The visual nature of the Windows operating system requires more advanced tools than were available a few years ago. Before windowed environments, a programming language was a simple text-based tool with which you wrote programs. Today you need much more than just a language; you need a graphical development tool that can work inside the Windows system and create applications that take advantage of all the graphical, multimedia, online, and multiprocessed activities that Windows offers. Visual Basic is such a tool. More than a language, Visual Basic lets you generate applications that interact with every aspect of today's Windows operating systems.


NOTE: Although Visual Basic is a comprehensive programming tool, VB retains its BASIC language heritage. Designers in the late 1950s developed the BASIC programming language for beginning programmers. BASIC was easier to use than other programming languages of the time, such as COBOL and FORTRAN. Microsoft never forgot VB's roots when developing Visual Basic. Newcomers to programming can learn to create simple but working Windows programs in just a short time. You will be using Visual Basic to write Windows programs before the next hour is complete.

New Term: Wizards are question-and-answer dialog boxes that automate tasks.

New Term: A compiler is a system that converts the program you write into a computer-executable application.

If you've taken a look at Visual Basic in the past, you'll be amazed at today's Visual Basic system. VB now sports a true compiler that creates standalone runtime .EXE files that execute more quickly than previous VB programs. VB also includes several wizards that offer step-by-step dialog box questions that guide you through the creation of applications. VB's development platform, a development environment called the Developer Studio, now supports the same features as the advanced Visual C++ and Visual J++ compilers. Therefore, once you learn one of Microsoft's Visual programming products, you will have the skills to use the other language products without a long learning curve ahead of you.

New Term:The Developer Studio is Visual Basic's development environment.

Languages

Programming languages today are not what they used to be. The language itself has not gotten less important; rather, the graphical interfaces to applications have gotten more important.

A computer cannot understand any person's spoken language. A spoken language, such as Italian or English, is simply too general and ambiguous for computers to understand. Therefore, we must adapt to the machine and learn a language that the computer can understand. VB's programming language is fairly simple and uses common English words and phrases for the most part. The language is not ambiguous, however. When you write a statement in the Visual Basic language, the statement never has multiple meanings within the same context.

New Term: Code is another name for the programming statements you write.

As you progress through the next 24 hours, you will learn more and more of the Visual Basic language's vocabulary and syntax (grammar, punctuation, and spelling rules). You will use the VB programming language to embed instructions within applications you create. All the code you write (code is the program's instructions) must work together to instruct the computer. Code is the glue that ties all the graphics, text, and processes together within an application. Code tells a checkbook application, for example, how to be a checkbook application and not something else. The program code lets the application know what to do given a wide variety of possible outcomes and user actions.

Visual Basics Three Editions

Visual Basic 5 comes in three flavors: the Standard Edition, the Professional Edition, and the Enterprise Edition. Although this book primarily deals with the Professional Edition, the Standard Edition is called the learning edition and provides the least expensive approach to using Visual Basic. The Standard Edition gives you a complete development environment, programming language, and many of the same tools the other editions offer. If you use the Standard Edition, you have a powerful programming tool. Some people develop only with the Standard Edition and never need anything else. Although this course targets the Professional Edition in an attempt to hit common ground, you will be able to utilize virtually the entire 24-hour course if you use the Standard Edition; you will find additional tools in the Standard Edition that this course does not even get to.

The Professional Edition offers a few more tools, including extra ActiveX add-in tools, better Internet programming support, a help file compiler, and improved database access tools. Most professional programmers use the Professional Edition.

The Enterprise Edition provides the client/server programmer with extended tools for remote computing and application distribution. Microsoft enhanced VB's performance for Enterprise Edition users working in a networked, distributed environment.


TIP:Most programmers need only the Standard or Professional Edition. The Enterprise Edition is aimed at developers who write network-intensive client/server applications. The Enterprise Edition is enhanced to aid such programmers who work within the special client/server environments.

If you want to create your own ActiveX controls, you will need the VB 5 Custom Control Edition that comes with the Professional and Enterprise Editions. If you use the Standard Edition, you're still in luck because the CD-ROM that comes with this book includes the VB 5 Custom Control Edition, which you can add to your Visual Basic folder and use to create ActiveX controls. Hour 21, "Visual Basic and ActiveX," describes more about the VB 5 Custom Control Edition.

The VB Programming Process

When you want to use Visual Basic, you'll follow these basic steps:

1. Start Visual Basic.

2.
Create a new application or load an existing application. When you create a new application, you might want to use Visual Basic's VB Application Wizard to write your program's initial shell, as you'll do in the next hour.

New Term: A bug is a program error that you must correct (debug) before your program executes properly.

3. Test your application with the debugging tools Visual Basic supplies. The debugging tools help you locate and eliminate program errors (called bugs) that can appear despite your best efforts to keep them out.

4.
Compile your program into a final application.

5.
Quit Visual Basic.

6.
Distribute the application to your users.

Rarely will you perform all these steps sequentially in one sitting. The six steps are not sequential steps, but stages that you go through and return to before completing your application.

Starting Visual Basic

You start Visual Basic from the Windows Start menu. The Visual Basic development environment itself usually appears on a submenu called Microsoft Visual Basic 5.0, although yours might be called something different due to installation differences. You will see additional programs listed on the Microsoft Visual Basic 5.0 submenu, but when you select Visual Basic 5.0 from the submenu, Visual Basic loads and appears on your screen.

On most systems, Figure 1.1's dialog box appears as soon as you start Visual Basic. The dialog box lets you start the VB Application Wizard, edit an existing VB project, or select from a list of recent projects you've worked on, depending on the dialog box tab you click.

As you can see at the bottom of the dialog box, you don't have to see the dialog box every time you start Visual Basic. If you click the option labeled Don't show this dialog box in the future, Visual Basic will not display the opening dialog box when you start Visual Basic.

Figure 1.1. The New Project dialog box often appears when you start VB.


NOTE:If you decide not to show the New Project dialog box for subsequent start-ups, you will still be able to access the dialog box's operations from VB's File menu.

Once you close the dialog box, the regular Visual Basic screen appears. As Figure 1.2 shows, VB's opening screen can get busy! Figure 1.2 shows the Visual Basic development environment, the environment with which you will become intimately familiar soon. From this development environment you will create Windows programs.

Figure 1.2. VB's screen looks confusing at first.

Although the screen can look confusing, you can fully customize the Visual Basic screen to suit your needs and preferences. Over time, you will adjust the screen's window sizes and hide and display certain windows so that your Visual Basic screen's start-up state might differ tremendously from that of Figure 1.2.

New Term: A dockable window is a window that you can resize and move to the sides of the screen and connect to other windows.


TIP:Most of VB's windows are sizable and dockable, meaning you can connect them together, move them, and hide them.

This hour's section titled "Mastering the Development Environment" explains the parts of the development environment and how to maneuver within it.

Stopping Visual Basic

You'll exit from Visual Basic and return to Windows the same way you exit most Windows applications: Select File|Exit, click Visual Basic's main window close button, press Alt+F4, or double-click VB's Control menu icon that appears in the upper-left corner of the screen.

If you have made changes to one or more files within the currently open project (remember that a project is the collection of files that comprise your application), Visual Basic gives you one last chance to save your work before quitting to Windows.


WARNING: Never power-off your computer without completely exiting Visual Basic, or you might lose some or all of your work for the current session.

Mastering the Development Environment

Learning the ins and outs of the development environment before you learn Visual Basic is somewhat like learning the parts of an automobile before you learn to drive; you might have a tendency to skip the terms and jump into the foray. If, however, you take the time to learn some of the development environment's more fundamental principles, you will be better able to learn Visual Basic. You then will be more comfortable within VB's environment and will have a better understanding of the related words when subsequent lessons refer to the windows and tools in the development environment.

Figure 1.3 shows the Visual Basic development environment with many of the more important screen components labeled. As you can see from the menu and toolbar, Visual Basic looks somewhat like other Windows programs on the market. Many of Visual Basic's menu bar commands work just as they do in other applications such as Microsoft Word. For example, you can select Edit|Cut and Edit|Paste to cut and paste text from one location to another. These same menu bar commands appear on almost every other Windows program on the market today.

Figure 1.3. Getting to know the development environment.


NOTE: Figure 1.3 shows only a portion of the development environment's windows and components. As you need additional tools, such as the Menu Editor, this tutorial describes how you access those tools.

Standards: The Menu Bar and Toolbar

Visual Basic's menu bar and toolbars work just as you expect them to. You can click or press a menu bar option's hotkey (for example, Alt+F displays the File menu) to see a pull-down list of menu options that provides either commands, another level of menus, or dialog boxes. Many of the menu options have shortcut keys (often called accelerator keys) such as Ctrl+S for the File|Save option. When you press an accelerator key, you don't first have to display the menu to access the option.

The toolbar provides one-button access to many common menu commands. Instead of selecting Edit|Paste, for example, you could click the Paste toolbar button. As with most of today's Windows applications, Visual Basic supports a wide range of toolbars. Select View|Toolbars to see a list of available toolbars. Each one that is currently showing will appear with a checkmark by its name.


TIP:As you begin to work with Visual Basic, pay attention to the form location and form size coordinates at the left of the toolbar buttons. These measurements, in twips (a twip is 1,440th of an inch, the smallest screen measurement you can adjust), determine where the Form window appears and its size. Twip values usually appear in pairs. The first location value describes the x-coordinate (the number of twips from the top of the screen) and the second value describes the y-coordinate (the number of twips from the left of the screen), with 0,0 indicating the upper-left corner of the screen. The first size value describes the width of the form, and the second size value describes the height of the form. Therefore, the size coordinate pair 1000,3000 indicates that the Form window will be 1,000 twips wide and 3,000 twips tall when the program runs. As you'll learn in the next section, the Form window is the primary window for the applications you write. The location and size coordinates describe the form's location and size when you run the application.

The Form Window: Where It All Happens

The Form window is your primary work area. Although the Form window first appears small relative to the rest of your screen, the Form window comprises the background of your application. In other words, if you write a Windows-based calculator with Visual Basic, the calculator's buttons all reside on the Form window and when someone runs the calculator, the calculator that appears is really just the application's Form window with components placed there and tied together with code.


NOTE:You will not see program code on the Form window. The Form window holds the program's interactive objects, such as command buttons, labels, text boxes, scrollbars, and other controls. The code appears elsewhere in a special window called the Code window. The Code window does not appear in Figure 1.3, but you can select View|Code to see the Code window. A Code window is little more than a text editor with which you write the programming statements that tie together the application.

Consider the sample program running in Figure 1.4's window. The window shows a simple dialog box with a few options, text boxes, and command buttons.

Figure 1.4. A simple dialog box produced from a running Windows program.

The programmer who created Figure 1.4's dialog box did so by opening a Form window, adding some controls (the items on the Form window that interact with the user--sometimes called tools), and tying the components together with some Visual Basic language code. That's exactly what you will do when writing simple and complex Visual Basic applications. You will begin with a blank Form window and add controls to the Form window such as options and command buttons. Perhaps your application might even require multiple Form windows.


NOTE: Some applications, such as Word, allow for several Form windows in a special mode called MDI (multiple-document interface) in which you can open multiple data documents within the same application. An application that requires only a single data window is called an SDI (single-document interface) application, such as the Windows Notepad application that lets the user open only one data document at a time. SDI applications might support multiple forms; however, these forms do not hold multiple data files but only provide extended support for extra dialog boxes and secondary work screens.

Compare Figure 1.4 with Figure 1.5. As you can see, Figure 1.5 shows the same application in the VB development environment. Figure 1.5 shows the application in its design-time state as opposed to its runtime state, which is shown in Figure 1.4. It is during design time that you design, create, edit, and correct the application. When you or another user runs the application, the results of your work can be seen.

Source program is code, forms, menus, graphics, and help files that you create and edit to form the project (also called source code).

The parts of the application that you create, such as the forms, the code, and the graphics that you prepare for output, comprise the source program. When you or another user compiles or runs the source program, VB translates the program into an executable program. You cannot make changes directly to an executable program. If you see bugs when you run the program, you must change the source application (which might contain multiple files in the project) and rerun or recompile the source.

Figure 1.5. The dialog box shown inside VB's development environment.

The Toolbox Supplies Controls

The toolbox contains the controls that you place on the Form window. All of Figure 1.5's controls appear on the toolbox. You will learn in the next hour's lesson how to place toolbox controls on the Form window. The toolbox never runs out of controls; if you place a command button on the Form window, another awaits you on the toolbox, ready to be placed also.

Figure 1.6 names every tool that appears on the standard Toolbox window. These are called the intrinsic controls because all three editions of VB support these standard tools. You can add additional controls to the toolbox as your needs grow. Some extra tools come with all three editions of VB, but these extra tools do not appear on the Toolbox window until you add them through the Project|Components menu option. If you use the Professional or Enterprise Editions, you will be able to add extra controls that don't appear in the Standard Edition's collection of intrinsic and extra controls.

Figure 1.6. The dialog box shown inside VB's development environment.

The Form Layout Window Places Forms

The Form Layout window displays the initial position and relative size of the current form shown in the Form window. For example, look back at Figure 1.5 to see the Form Layout window. The application being studied is a multiple-form application. The form with the title Text Box Properties is just one of several forms. The Form Layout window always shows where the form appears in the current Form window. If you want the form to appear at a different location from the current position, you can move the form inside the Form Layout window to move the form's appearing position when the user runs the application.


TIP: Notice that the form location indicators, to the right of the toolbar buttons, change when you move the form in the Form Layout window.

This book generally does not show the Form Layout window in figures to give more room to the Form window and its contents. You can display the Form Layout window from the View menu, and you can hide the Form Layout window by clicking its window close button.

The Project Explorer Window

The Project Explorer window, often called the Project window, gives you a tree-structured view of all the files in the application. Microsoft changed the formal name from Project window to Project Explorer window between versions 4 and 5 to celebrate the resemblance of the window to the typical Explorer-like tree-structured file views so prevalent in Windows 95 and NT. You can expand and collapse branches of the view to get more or less detail.

The Project Explorer window displays forms, modules (files that hold supporting code for the application), classes (advanced modules), and more. When you want to work with a particular part of the loaded application, double-click the component in the Project window to bring that component into focus. In other words, if the Project Explorer window displays three forms and you need to edit one of the forms, locate and double-click the form name in the Project window to activate that form in the Form window. Figure 1.7 shows a Project Explorer window that contains several kinds of files.

Figure 1.7. The Project window keeps track of the project's components.


WARNING: If you add a help file to your application, the Project window does not display the help file.

The Properties Window

New Term: Properties are detailed descriptive information about a control.

A different list appears in the Properties window every time you click over a different Form window tool. The Properties window describes properties (descriptive and functional information) about the form and its controls. Many properties exist for almost every object in Visual Basic. The Properties window lists all the properties of the Form window's selected control.

Help Is at Your Fingertips

New Term: Books Online are electronic books about Visual Basic for the Visual Basic programmer.

Visual Basic's online help system is one of the most advanced on the market. When you want help with a control, window, tool, or command, press F1. Visual Basic analyzes what you are doing and offers help. In addition, Visual Basic supports a tremendous help resource called Books Online. When you select Books Online from the Help menu, Visual Basic displays a tree-structured view of books about Visual Basic that you can search and read. The online help extends to the Internet as well. If you have an Internet connection, you can browse the latest help topics by selecting Help|Microsoft on the Web.

Summary

This hour quickly introduced you to Visual Basic. Perhaps you already can see that Visual Basic is more than it first appears. Programmers use Visual Basic to create extremely advanced Windows applications. Now that you understand VB's purpose and how to start and stop VB, you're ready to jump right in.

The next hour describes a sample application that comes with Visual Basic so you can get a better picture of how Visual Basic's components work together.

Q&A

Q Must I learn a new language to use Visual Basic?

A
Visual Basic is more than just a programming language. Nevertheless, learning VB's language portion is integral to writing advanced applications. Fortunately, the Visual Basic programming language is one of the easiest programming languages in existence. The language is simple but powerful because Microsoft based Visual Basic's language on BASIC, a beginner's language. VB's simplicity does not translate to inability, however. Visual Basic is one of the most powerful Windows programming languages on the market and supports advanced programming techniques.

Q How can I master the complicated-looking development environment?

A
The Developer Studio might look complicated, but only because you are new to the development environment. The development environment is little more than a collection of windows. As you learn more about Visual Basic, you will learn which windows you need and when you need them; you can close the other windows. The Developer Studio development environment is a development platform Microsoft has integrated into most of its language products, including Visual C++ and Visual J++. Therefore, once you master the development environment, you will already understand the other language's development environment as well.

Workshop

The quiz questions and exercises are provided for your further understanding. See Appendix C, "Answers," for answers.

Quiz

1. What is the purpose of Visual Basic?

2.
How have programming languages changed over the years?

3.
What programming language is Visual Basic based on?

4.
Which Visual Basic development environment window forms the background for the applications you develop?

5.
What are the three editions of Visual Basic and how do they differ?

6.
What is the difference between the Form window and the Form Layout window?

7.
How can you tell the width and height of the Form window?

8.
True or false: All the tools you find on the Toolbox window when you start Visual Basic are the intrinsic controls.

Exercise

Start Visual Basic and select various options from the View window. You will see several new windows open. Look through the menu options and click on any scrollbars you see. Don't save anything when prompted. Double-click over tools on the toolbox to see different tools appear on the Form window. Move the tools away from the center of the window to see more of them at one time. As you click on different tools in the Form window, watch the Properties window change to reflect the current tool's properties.