Latest News

WelCome To Visual Basic

Day 1. Welcome to Visual Basic
Visual Basic 6 is Microsoft's latest and greatest version of the Visual Basic programming language. Although writing
programs can be a tedious chore at times, Visual Basic reduces the effort required on your part and makes
programming enjoyable. Visual Basic makes many aspects of programming as simple as dragging graphic objects onto
the screen with your mouse.
Today begins your 21-day Visual Basic tutorial. Before today ends, you will have created your very first Visual Basic
application. In the next three weeks, you will master Visual Basic 6, and you will be able to develop applications that
do work you need done.
Today, you learn the following:
Visual Basic's history
The programming design and authoring process
How Visual Basic's visual interface makes programming easy and fun
The Application wizard
Why event-driven programming is so important to a Windows environment
Visual Basic's Background
By understanding the background of Visual Basic, you'll gain insight into Visual Basic 6 and you'll be better equipped
to use Visual Basic. Microsoft based Visual Basic on a programming language written for beginners called BASIC.
BASIC has been around for more than 35 years in one form or another. The original language designers wanted to
develop a programming language that beginners could use. With BASIC, new programmers could become proficient
right away. Other programming languages of the day, such as COBOL, FORTRAN, and Assembler, required much
more study than BASIC before one could use them effectively.
BASIC stands for Beginner's All-purpose Symbolic Instruction Code. That's some abbreviation!
A programming language is a set of commands and command options, called arguments, that you use to give
instructions to the computer. Computers cannot (yet) understand human languages because people deal well with
ambiguous commands, and a computer cannot understand such ambiguity. A programming language must be more
precise than a spoken language.
Note
Programming languages are easier to learn than foreign languages. Computer languages often have fewer than 300
commands, and many of those commands are words you already understand, such as Open and Next.
Although the BASIC language was designed for beginners, a BASIC program was still rather cryptic and required
study. Listing 1.1 shows a program written in BASIC. The program's goal is to print the mathematical squares for the
numbers 1 through 10. Although you can probably guess at many of the program's logic and commands, the program is
certainly not the clearest piece of literature in the world and requires that you understand BASIC before you can fully
comprehend the reason for all of its elements.
Programs are often comprised of several files that interact with one another, so you'll often see the term application
used as a synonym for all of a program's files. The program, or application written in a programming language, is a set
of instructions that directs the computer.
Listing 1.1 Early BASIC programs had line numbers and were somewhat cryptic.
10 REM This program computes and prints the first ten squares
20 CLS
30 PRINT "Squares from 1 to 10"
40 PRINT "Value", "Squared"
50 FOR N = 1 TO 10
60 PRINT N, (N*N)
70 NEXT N
80 PRINT
90 END
Do Don't
DON'T fret over all this talk about squaring numbers from 1 to 10. Don't like math? No problem! Visual Basic
Do Don't
will do all the math you need done.
If you were to run the BASIC program, here is the output you would see:
Squares from 1 to 10
Value Squared
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Notice that BASIC is strictly a text-based language. Both its program and output are textual and do not produce the
graphical, windowed output that today's programs produce.
Microsoft did not create Visual Basic directly from the original BASIC language. Although the BASIC language
evolved through several stages over its 35-plus year history, it kept its original structure in most of its incarnations.
When Microsoft decided to use BASIC as its primary programming language supplied with the original MS-DOS
operating system, however, it honed the BASIC language and added functionality to BASIC by creating several
incarnations of BASIC with names such as MBASIC (for Microsoft BASIC), GWBASIC (for, some say, Gee-Whiz
BASIC), BASICA (for BASIC Advanced), QuickBASIC, and QBasic (which is still supplied on Windows operating
system CD-ROMs).
Throughout BASIC's evolution, the BASIC language kept its simple nature while gaining powerful new commands
along the way. The text-based nature of languages such as QBasic helps new programmers get up to speed more
quickly than many nontext languages such as Visual C++ do. To maintain this ease of use, Microsoft wanted to keep all
its BASIC language versions interpreted in nature as opposed to compiled. A programmer can execute a program based
on an interpreted language immediately and see results and errors instantly. Such feedback is critical for beginners who
need a quick response when learning how to program. Compiled languages, although they run faster and are better
suited for commercial program development environments, require much more effort to work with.
An interpreted language, such as BASIC, lets you run programs as you write them. Interpreted languages make good
learning platforms because of their quick feedback. A compiled language requires extra steps, called compilation and
linking, before the programmer can run the program. The compiled program resides in the computer's own native
language and not in the programming language that the programmer originally used.
As Windows became more popular, Microsoft realized that the text-based QBasic would not work as a windowed
programming language. Microsoft developed Visual Basic, a language based on BASIC but one much more suited to
today's windowed environments. Whereas QBasic and all other BASIC incarnations were text-based, Visual Basic is
graphical. Although a Visual Basic program might contain code that looks somewhat like the program in Listing 1.1,
the majority of a Visual Basic program consists of graphical elements that have little resemblance to the text-based
code in Listing 1.1. Figure 1.1 shows a Visual Basic screen that contains many pieces from a Visual Basic program.
Figure 1.1. The Visual Basic programming screen can look busy, but it is simple to use.
Code is another name for a program's set of instructions.
Note
Well before you finish the book, you'll understand every item inside Figure 1.1. Although the screen looks busy and
overwhelming, Visual Basic is simple to understand.
In addition to being graphical and simple to use, Visual Basic has become one of today's most popular languages
because it is both interpreted and compiled! You can test a Visual Basic program that you write by running the program
interpretively until you get all the bugs out. Once you eliminate the bugs and thoroughly test your program, you then
can compile the program into a fast and secure (nobody can easily modify the program) executable program that you
can distribute to others to use. By making the compilation process a simple menu option, Visual Basic handles the more
difficult compilation steps (including something cryptic called link editing) that other languages used to require you to
go through.
A bug is a program error. If a program that you write does not work properly, you will need to debug the program by
removing all the bugs.
About the time Microsoft released the first version of Visual Basic, many people were predicting the demise of the
BASIC language (and its offshoots such as QBasic). These naysayers thought any language based on BASIC could not
be used for serious programs because they never thought of BASIC as a serious language. Languages such as C, C++,
and Pascal were all the rage because of their compilation abilities and also because their programming structures lent
themselves more to a Windows environment. With Visual Basic, Microsoft taught the programming community these
and other lessons:
A BASIC-like language can be both simple to understand and powerful.
With the right interface, a BASIC-like language works well for a Windows environment.
Visual Basic can be both an interpreted and a compiled language depending on the programmer's requirements.
Instead of being obsolete, a language based on BASIC can become one of the most widely used languages in
the world.
Visual Basic's Visual Nature
As you saw in Figure 1.1, Visual Basic 6 is more than just a programming language. The secret to Visual Basic is in its
name: visual. With today's Windows operating systems, a program must be able to interact with the screen, keyboard,
mouse, and printer graphically. Older programming languages, such as BASIC, worked well in a text-only computing
environment, but such languages do not support the graphical interface needed for today's computers.
You won't even learn much of the Visual Basic programming language in the first week of this tutorial because much
of the Visual Basic programming process requires interacting with the Visual Basic visual environment and requires
very little of the programming language details to make working programs. Only when you need to write more
advanced programs will you need to learn more of the Visual Basic language than just the handful of commands you
learn in your first few days.
Note
It's not just the underlying BASIC language that makes Visual Basic simple to learn and use. Much of a program's
development consists of dragging and dropping (with your mouse) elements onto the Visual Basic screen when you
create a program. Instead of writing a series of complicated input and output statements to interact with users, you will
drag controls, such as text boxes and command buttons, onto the screen; Visual Basic takes care of making the controls
operate properly when the user runs the program.
A user is a person who uses a program. You, the programmer who writes programs, are also a user because you use
programs that you and others write. The Visual Basic programming system is nothing more than a program that you use
to create other programs.
Visual Basic comes in several varieties including the following:
Visual Basic Enterprise Edition: Created for team programming environments and client/server computing
where applications distribute processing and data among several computers.
Visual Basic Professional Edition: Geared toward professional programmers who want to get the most from
the Visual Basic programming environment. This edition includes a full set of tools and wizards that help you
package and distribute applications. This 21-day tutorial assumes that you use the Professional Edition as most
Visual Basic programmers do. Nevertheless, if you use one of the other editions, the majority of this book also
applies to you because this tutorial does not focus on the Professional Edition-only tools as much as it presents a
well-rounded introduction to the Visual Basic programming environment and language.
Visual Basic Learning Edition: The essentials with the standard complement of programming tools and
everything one needs to get started programming. A multimedia CD-ROM called Learn VB Now comes with
the package as well as a full set of Microsoft Developer Network documentation so that you will have the help
that you require to learn and use Visual Basic.
Note
A special edition of Visual Basic comes with a package called Visual Studio. Visual Studio is a programming
environment that supports several Microsoft languages including Visual Basic, Visual C++, and Visual J++. When you
use Visual Basic, you use the same environment that users of these other languages also use. Therefore, if you move to
another programming language, you will not have to master a new set of menus and dialog boxes.
Why Write Programs?
Many computer users will never need to learn computer programming. Some people buy all their programs from the
store or from mail-order outlets and never need more specialized programs. Rarely, however, will you be able to find
exactly the program you need for a particular task, especially if you use a computer to help you in your business or
scientific research. In addition, you might think of a new game concept that you want to turn into a hot-selling
computer game so that you can retire early in the Cayman Islands. If you want a specific application but cannot find
what you need on the store shelves, or if you want to write new programs for a living, you'll need to design and write
those programs using a programming language such as Visual Basic.
Note
Remember that you cannot just tell a computer what to do and expect it to work for you. A computer must have a
detailed list of instructions because the computer is a dumb machine that does nothing on its own. You give your
computer those instructions in the form of a program. A Visual Basic program consists of program code (similar to that
in Listing 1.1) and visual elements that define the screen and the Windows controls that the program's user interacts
with when the user runs the program.
Tip
When you learn Visual Basic, you also learn how to automate common application programs such as those you find in
Microsoft Office. Microsoft Office is comprised of several programs that work together, such as a word processor,
worksheet, and database program. Microsoft Office also contains the complete Visual Basic 6 programming language
with which you can automate Microsoft Office applications. (Microsoft Office 95, the edition that preceded Microsoft
Office 97, contain Visual Basic for Applications (VBA), which is similar but not fully compatible to Visual Basic
version 6.) For example, you can automate your accounting month-end procedures by writing a program that
consolidates your month-end Excel worksheets. The Visual Basic that comes with applications is not the full Visual
Basic development system you get with Visual Basic 6,but it does contain the complete language so that you can fully
control the applications.
The Programming Process
Over time you'll find your own way of writing programs that works best for you. Nevertheless, you'll generally follow
these standard set of steps when creating your Visual Basic programs:
1. Decide what your application is to do by creating an overall design.
2. Create the visual portion of your application (the screens and menus that your users will interact with).
3. Add Visual Basic programming language code to tie the visual elements together and to automate the program.
4. Test your application to locate and remove any bugs you find.
5. Compile your tested application and distribute the compiled application to your users.
Do Don't
DO test your application to rid it of bugs and then distribute
your application program to others. Despite the virtual
impossibility of eliminating all bugs, do `test, test, and test again
trying all possibilities in the program to help ensure that you've
found as many bugs as you can before you compile and
distribute your program.
DON'T be surprised if a user locates another bug (or
several of them). The most thorough testing never
guarantees that all bugs are gone. The more that your
program does, the more likely a bug will raise its
ugly head some day when you and your users least
expect it.
By waiting until you've thoroughly tested your Visual Basic application program before you compile the program, you
help speed up the testing process. When you test your program interactively, you can locate and correct bugs that you
find more easily and quickly. Visual Basic includes a special helper system called a debugger that you can use to help
you locate bugs that appear during testing. You'll learn in Day 21, "Distributing Your Applications," how to use the
debugger.
A debugger is an interactive monitoring system that you can turn on and off inside Visual Basic that helps you locate
statements that contain bugs. For example, if you run a program you've written and the program computes an amount
incorrectly, the debugger can help you quickly find the statement in the program that contains the bug.
Before Visual Basic, writing a program was more tedious for several reasons. In a text-based environment, you would
have to design on paper all the screens that the user would see. You would then take that paper to the users to see if you
were designing exactly what they wanted. If you were designing a program for mass distribution, such as a game or a
general-purpose business application, you would still write down all the screens, create complicated data flows to and
from the various screens, design the disk files needed by the program, and basically plan every detail before you ever
went to the keyboard.
Visual Basic's visual nature encourages you to go to the keyboard much earlier in the programming process. Instead of
using paper, you'll design screens with Visual Basic's tools. Figure 1.2 contains one such screen. No code is required to
produce a screen such as this one; all you need to do is drag the various controls onto the Form window.
Figure 1.2. Visual Basic enables you to design and create screens as you create your program.
The Form window, also called a form, comprises the background of a Visual Basic program's screen and contains
elements such as command buttons and scrollbars. Programs may require one or more form windows depending on the
nature and complexity of the program.
Even before you add code, you can test your program screens (each form is the basis for a screen) because Visual Basic
enables you to run your program interactively after you create at least one form. You can make sure that your screens
look good, and you can show your prototype to users who have requested the program to ensure that they like what you
are creating. Making changes in this prototype pre-coding stage is much easier than making changes after you add the
code. Visual Basic's prototyping capability is one way Visual Basic helps you create programs quickly and accurately.
A prototype is a test program that contains virtually no functionality but does contain some or all of the screens that the
final program is to contain. You and your program's ultimate users can test the prototype to see whether you are
including all of the needed screen elements.
Tip
Once you create your program, test your program, compile your program, and distribute your program to your users,
you still can make changes to the program. Doing so, however, is tedious and requires that you re-distribute all the
application's files once again to the user. Nevertheless, the earlier you locate problems, the simpler those problems are
to repair.
Understanding Program Maintenance
Bugs are not the only reason that you will work on a program after you think you're completely done with it. Program
maintenance is necessary because requirements change, companies change, and laws change. You must also change the
programs you write so that they remain viable programs; you will need to update your program periodically to reflect
changes that impact the program. In addition, users will think of new things that they want the program to do.
Program maintenance is the term used for the updating of a program after the program is put into use. This update may
be a result of a user's request or a change in the way the program needs to operate.
It is said that a program is written once and modified many times. The more program maintenance you perform, the
more likely that your program will be up-to-date and in use. You may want to release new versions of your program so
that users can, with a different version number on the opening screen that you place there, keep track of the latest
version installed on their system.
Tip
Document your programs so that other programmers will understand your code if they must make changes to it later.
As you learn more about the Visual Basic programming language, you'll learn how to write code that is clear, and you'll
learn how to create documentation for your program. The more remarks you put in your program and the clearer you
write program code instead of using tedious, complicated, tricky program statements, the easier it will be for you and
others to track errors and maintain the program later.
Documentation is comprised of descriptions of the program. You can place documentation inside the program itself so
that when you (or someone else) later make a change to the program, you'll read what sections of the program are for
without having to figure out what the code's purpose is. Internal Visual Basic program descriptions are called remarks.
Add program remarks as you write your program because it is at that time that you understand the program the best. If
you wait until after you complete an application, as many programmers do, your application might never be properly
documented because other projects can take over your time, and the documentation is often pushed aside once a project
is completed.
In addition, you may want to write external documentation with screen shots of the program's different screens and
descriptions of what the user must do to start, use, and terminate the program. The better your user's documentation is,
the more likely your user will master your program and want to use more programs that you write.
Creating Your First Program
If you are familiar with several other Windows products, such as Microsoft Publisher, you've see wizards that work
with you, helping you create the documents you require. Visual Basic also supports wizard technology to help you
create programs. When you write a Visual Basic program, you have a choice to create an application from scratch or
use a wizard to create an application's shell or general structure. After the wizard creates the application's shell, you can
fill in the details.
A wizard presents step-by-step questions and prompts that you respond to. As you respond, the wizard generates an
application that matches the criteria you specify. Visual Basic offers several wizards, but the one you'll use most
frequently is called the Application wizard.
It's sometimes difficult to tell whether you should create an application shell with the Application wizard and then fill
in details for your particular situation or create an application from scratch. Some people, if they've created another
application already that is similar to the one they need, make a copy of the first one and make changes to the copy to
create the new application. Over time, you'll learn to decide which is best for your needs in different situations.
To help you get started, this section guides you through the creation of your very first application. You'll see how easy
the Application wizard is to use for an application's shell. Although the resulting application will not do much (it's only
a shell after all), you will see how much Visual Basic can automatically create when you use the Application wizard.
By tomorrow's lesson, you will be ready to learn how to create an application from scratch without the Application
wizard.
Note
Perhaps surprisingly, you'll probably create more applications from scratch instead of using the Application wizard, or
you'll make a copy of a similar application and modify the copy when making a new program. Although the
Application wizard creates a fully-functioning program skeleton, you'll develop your own style of programming over
time, and you'll probably find it easier to modify a copy of an existing application than first creating a skeleton and
adding to it. Your preferred style will come with time, so just sit back and enjoy learning Visual Basic. Try things, don't
be afraid to mess up, and expect some errors every time you write a program. Programming is creation, and you'll find
that Visual Basic makes creating fun.
As soon as you start Visual Basic, the Application wizard is there to help. The New Project dialog box, shown in Figure
1.3, appears when you start Visual Basic from the Windows Start menu. The tabs on the New Project dialog box offer
these choices:
Figure 1.3. You can select the Application wizard from the New Project dialog box.
New lets you create new applications by using various wizards or starting from scratch.
Existing lets you select and open an existing Visual Basic project.
Recent displays a list of Visual Basic projects you've recently opened or created.
Note
If you cancel the New Project dialog box, and then later want to start the Application wizard, select File, New Project to
display the New Project dialog box once again. This New Project dialog box will not contain the Recent and Existing
tabbed pages, however, because you are specifying from your menu choice that you want to create a new project.
A project is a collection of files that make up your application. A single application might consist of several files, and
the project is the collection of those files. One or more of the files might contain code, one or more of the files might
contain descriptions of screens inside their respective form windows, and one or more of the files might contain
advanced programming information that your program will use to communicate with other programs and modules
inside the operating system.
Do Don't
DO, if you don't want to see the New Project dialog box every time you start Visual Basic, click the check box
labeled Don't Show This Dialog Box in the Future. The next time you start Visual Basic, the New Project dialog
box will not appear.
When you select the icon labeled VB Application Wizard on the New tab, the wizard begins its work. The first screen
that the wizard displays is an introductory title screen that explains the wizard is about to begin. (In addition, the screen
lets you load another profile that defines options, but you won't need extra profile options for this book.) As with most
wizards, when you finish reading and selecting from one wizard screen, you click the Next button to display the next
screen that the wizard has to offer. Figure 1.4 shows the next wizard screen from which you must select an interface
type.
Figure 1.4. The interface type determines how your application will process multiple windows.
Here are the options from which you can select:
Multiple Document Interface (MDI) allows your application to contain multiple document windows. In
effect, this interface lets you work with several sets of data in multiple windows within your program. Each
document window is called a child window.
Single Document Interface (SDI) limits your application to one open document window at a time. Most
applications that you write will probably be SDI applications.
Explorer Style lets your application take on a Windows Explorer-like interface with topic summaries in the left
window and details for a selected topic in the right pane.
You can click any of the three options to read a description and see a thumbnail sketch of a sample program window.
Many of your applications will contain the single document interface because many applications require only a single
window with data that is open at any one time. For this first example, select the Single Document Interface option.
The wizard screen also lets you name your project. The default name, Project1, leaves a lot to be desired, so change the
project name to FirstApp (no spaces are allowed), and click Next to display the next wizard window shown in Figure
1.5.
Figure 1.5. Select the options you want your application's menu to contain.
The Application wizard adds the menu options that you select to your application menu. The options are common
Windows options found on most Windows programs. The menus will be the typical Windows drop-down type. You
can select the menu bar options (such as File, Edit, and so on) as well as submenu options, such as New, Open, and
Close. The ampersand (&) next to a letter in a menu name indicates the underscored accelerator key letter; in other
words, &New indicates that New (notice the underscore) appears on the menu and that the user can select the option by
pressing Alt+N. If you want to place an actual ampersand in the name, use two; for example, typing A&&B produces
A&B. For this application, leave all the options as they are (keeping the File, Edit, View, Window, and Help options
checked) and click Next to continue with the wizard.
Note
After the Application wizard finishes creating your application, the menu options will operate as expected. For
example, the File menu will appear when you select Alt+F or click the File menu.
The next wizard screen, shown in Figure 1.6, lets you select the toolbar buttons that your application will have. As you
can see, the Application wizard does a lot of work for you. By creating an initial toolbar, the wizard takes care of a lot
of tedium that you would otherwise have to handle. The left window pane indicates the available toolbar buttons and
the right window pane lists the buttons (and separator spaces between buttons) on your application's toolbar. As with
the menu options in the previous screen, click Next to accept all the default toolbar settings.
Figure 1.6. The Application wizard saves you time by creating an initial toolbar.
The next wizard screen to appear is the Resource screen from which you can elect to use resources in your program,
such as multilanguage text files. Simple programs often do not require external resources. For this example, keep the
option labeled No checked and click the Next button to continue.
The next wizard screen is the Internet Connectivity screen from which you can add an Internet interface to your
program if you want one. If you were to select Yes from this window (please don't select Yes here), the Application
wizard would add a complete Internet browser to your application that would operate much like Internet Explorer.
Without any programming on your part, your application's user can access the Internet. When the user enters an
Internet address (also known as an URL [pronounced "earl"] for Uniform Resource Locator), such as
http://www.mcp.com, the browser displays that Web page in the application's browser window, first logging on, if
needed, using the PC's default Internet service. You can enter a default startup page address that initially displays when
the user starts the browser.
Caution
If you add the browser to your application, you are assuming that your user has Internet access. If not, an error will
result when the user attempts to use the browser.
This first application requires no Internet access, so click Next without changing any of the default options to display
the next wizard screen. The screen gives you the option of adding one of these standard screens to your application:
Splash screen is an opening title screen that appears when your application first begins.
Login dialog is a dialog box that asks for the user's ID and password as a part of application security that you
can add.
Options dialog is a tabbed blank dialog box from which your users can specify attributes that you set up for the
application.
About box is a dialog box that appears when your users select Help, About from the application menu.
For this application, click the option labeled About Box.
Tip
The button labeled Form Templates lets you select from several form templates located in the Visual Basic Templates
folder. Visual Basic installs the templates you select into your application. The templates include an add-in template
that lets you add a form from your own library, an ODBC Log In form that lets your users connect to advanced
database access, and a Tip of the Day that displays a random tip when your user starts the application.
A form template is a model of a form that you can customize. Form templates are forms with similar properties that
might appear in several different applications.
The After selecting the About Box standard form, click Next to bypass the database wizard screen that lets you add
external database files to your application. You can click the button labeled Finish to instruct Visual Basic to complete
your initial application.
Note
The View Report button displays a summary of the project you have designed, and details the changes you can add and
other wizards that you can run to add functionality to your new project.
Congratulations! You've just created your first application without knowing much about Visual Basic and without
knowing any of the Visual Basic programming language! After a few gyrations on the screen, Visual Basic displays a
dialog box letting you know that your application is complete. When you click OK, the dialog box disappears, and you
can run your application.
Tip
After loading an application from disk or creating one, run or execute that application to see it work just as your users
will eventually do after you've tested and compiled the applications you write. Visual Basic is a lot like a kitchen. You
are the cook, and your application is the recipe. Change the application (the recipe), and the resulting program (the
meal) turns out to be different. The programming stage can take quite a while if your application is complex even if you
use the Application wizard to generate an initial program. As you create the program, you won't see the program do
work until you run it.
Run the program (the program runs interactively by default) by selecting Run, Start. You'll see from the menu option
that F5 is a shortcut key for running the application as well. Figure 1.7 shows the window that appears.
Figure 1.7. Your first application is complete!
With the Application wizard, you created a fully working program (albeit only a simple shell that only does a little) just
by answering the wizard's screen prompts. You have created an application that does the following:
A standard program window appears that you can resize and move. The name of the project, FirstApp, appears
in the window's toolbar.
A status bar displays the date and time. You can turn on and off the status bar from the View menu.
A working menu appears with four options. Only the Help, About menu option does work (try it), but the usual
menu options, such as File, Open (produces a file locating dialog box) and Edit, Cut, are all there ready for you
to insert active code behind them. The About dialog box follows the standard Windows convention of
displaying system information when you click its System Info button.
Tip
The System Information screen displays a complete summary of the user's operating system and hardware. This
summary appears after Visual Basic executes a System Info program that searches the user's computer for specific
hardware and system information. (You can call the System Info program from locations other than the About box.)
Such a summary can some in handy when your users call you with problems about applications you write. You can ask
the user to display the system information summary to verify that the user is using the proper operating system and
hardware that your program requires. In addition, the System Info window is useful for checking available resources
such as disk space and memory to ensure that your PC is running with enough resources.
A standard toolbar appears that you can add functionality to and turn on and off from the View menu.
The application does little, yet it is complete and ready for you to fill in the blanks. You can easily change and add to
the application, its menus, and its windows. The application is only a shell of things to come, yet the Application
wizard generated a complete project that takes care of much tedious work that you might otherwise have to add by hand
if you created the application from scratch. You'll find in tomorrow's lesson that you can create working projects quite
easily, but the Application wizard adds basic functionality that applications often require.
To quit the running application, select File, Exit. Answer No to the prompts when Visual Basic asks if you want to save
the project. You don't need to save the application shell because you can easily generate this project again by running
the Application wizard once again.
Event-Driven Programming
Figure 1.8 shows a window from a Windows program. The window contains several kinds of Windows controls such
as command buttons, check boxes, and a scrollbar. These controls are just a sample of the many Windows controls
available for you within the Visual Basic programming environment to add to the programs that you write.
Figure 1.8. Windows programs respond to events.
Visual Basic's visual nature requires these kinds of controls because, unlike programs written in older text-based
languages, Windows programs must respond to events. An event might come to a program from any of these controls,
as well as from internal activities such as the PC's clock. Events come in random order. For example, the user of Figure
1.8's window might click a command button or check one or more check boxes, or open the drop-down list box. The
user might perform several of these events in a different order each time the user runs the program. You must use
event-driven programming techniques to respond properly to the user's actions and other activities that trigger events.
An event is an activity that occurs during a program's execution, such as a mouse click or a keystroke. Event-driven
programming applies to programming that responds to Windows events.
Note
Not only must your program handle random events, but if more than one Windows program is running at once, each
program needs to analyze and respond to events.
As Figure 1.9 shows, Windows handles a few events but passes most to the programs currently running. Windows is a
multitasking operating system so more than one program can run simultaneously. Your program must handle any and
all events appropriate at the time the events occur and ignore all the others. For example, if your program needs to
display a warning message at a preset time interval, your program will have to check the timer event to see whether the
correct time span has passed since the last warning. If another program running at the same time did not require the
timer, that program would ignore all timing events that Windows sends to it.
Figure 1.9. Your programs must respond to some events and ignore others.
A Visual Basic program consists of the visual interface that makes up the windows and controls that the user sees and
interacts with. In addition, programming code connects everything together. Each control is both automated and set up
to respond to the programming code. For example, a command button will visually show a click action when the user
clicks the button with the mouse when running the program. You have to do nothing more than place the button on the
form (the program's window) for the button to operate. (As with all command buttons, you can trigger a command
button with the Enter key as well as the mouse.) Other aspects of the command button, however, are under your
control, such as the name or picture that resides on the button, the size of the button, the color of the button, and so on.
These are properties that you can change, although Visual Basic assigns default values. Properties distinguish one
command button from others.
A property helps to differentiate a control from other controls because the property shows appearance and behavior of a
control. Properties have values, such as colors, text labels, size, and location on the form. When you place a control on
a form, you assign properties that make that control somehow unique from the other controls.
Figure 1.10 shows a window with several command buttons. If all of these buttons appeared in a program with no code
behind them to modify the way they respond, you could click any of them and they would all respond the same way,
depressing inward with the click and triggering a click Windows event. Despite these similarities, however, each button
looks differently because its text caption, size, location, color, and text font property values are different from the other
buttons.
Figure 1.10. Multiple controls look different if they have different property values.
Once you place controls on a form and assign their individual property values, you are ready to write programming
code that responds to events. The same control can trigger several different kinds of events. For example, a command
button may generate a single-click or a double-click event depending on what the user does. The code that you write for
the program determines which of those events your program ignores or handles in some way.
Tip
If you write code for a particular event, your program will respond to that event when it occurs during the program's
execution. If, however, you don't write code for a particular event, and that event happens, your program will ignore
that event when Windows sends it to your program.
Your programming code behind the forms looks and acts, not like one long listing of text, but like several small
sections of code with each section written to respond to one of the form's control events. Each of those sections sits
around doing nothing until its event occurs; at that time, the program immediately starts executing that event's code.
For example, if the right-click of an object, such as a particular command button, is to produce a warning beep and
message, you must write the code to produce the beep and message. The executing program runs that code if and only
if the user right-clicks over the button.
An object is an element from a Visual Basic program, such as a control, form, or code module that holds programming
statements.
How do all these details work together? The answer to that will take, oh, about 20 more days. Tomorrow's lesson
begins to show you how to specify control properties and how to respond to those controls when you create your very
first Visual Basic program from scratch without the help of the Application wizard. Theory alone is not enough—you
need to get your hands on the keyboard and begin placing controls, setting control property values, and writing code
that responds to events.
Summary
You are well on your way to mastering Visual Basic. Today, you learned the background needed for programming. By
understanding the programming process, you are better equipped to begin using Visual Basic, one of the most advanced
programming environments available today.
Today's lesson explained how you design and write programs. Visual Basic has changed the way programmers design
programs because the Visual Basic environment makes it easy to prototype your program's design and then turn that
prototype into a finished application. Programming often requires several review and edit steps. Programs rarely work
perfectly the first time you write them, but as you saw today, Visual Basic's interactive environment takes care of much
of your work so that you can keep errors to a minimum.
The Application wizard will generate a program shell that you then can add details to so that the program becomes a
separate, working application that performs its needed job. Those details consist of adding controls, setting property
values, and writing code to make the program interact and respond to the controls properly. The rest of your 21-day
tutorial will show you how to fill in those details to make working programs.
Q&A
Q: Do I always follow the programming process steps (design, create visual elements, and so on) for all
Visual Basic programs I write, or just for the small ones?
A: The larger the program, the more you'll need to adhere to the program development procedure. Programs get
complex quickly as you add more and more features to their requirements. One feature may affect other
features, so the more you plan, the less you'll have to redo and correct later. Fortunately, the Visual Basic
environment makes program changes rather simple in many cases, even changes that involve major structural
design changes. Of course, if you start with an Application wizard's shell, the design of your program is your
second step. As you learn how to write programs throughout this book, you'll learn more about proper
program design and creation.
Q: Does the Application wizard generate program code?
A: The Application wizard does generate some code but not much. The purpose of program statements is to
make the program perform a specific function, such as computing accounting figures or processing customer
billing. It's your job as the programmer to specify the code.
Workshop
The Workshop provides quiz questions to help you solidify your understanding on the material covered and exercises
to provide you with experience in using what you've learned. You should understand the quiz and exercise answers
before continuing to the next chapter. Appendix A, "Answers to Exercises," provides the answers.
Quiz
1: What language did Microsoft use as the basis for Visual Basic?
2: Why is Visual Basic suitable for both beginners and advanced programmers?
3: Which is more important to newcomers to Visual Basic: the programming language or the visual interface?
4: What's the difference between a form window and the application window?
5: What do the terms bug and debug mean?
6: Which runs faster: a program written in an interpreted language or a program written in a compiled
language?
7: Which is easier to debug: a program written in an interpreted language or one written in a compiled
language?
8: What is the difference between a splash screen and a Tip of the Day screen?
9: What's the difference between a control and a control property value?
10: True/False. Controls hold code that makes them respond to the user's input.
Exercise
Use the Application wizard to create an application that includes an Internet browser window and a splash
screen in addition to the other options you selected today when you created your first project. Run the
application to see how the Internet access works. If you don't have Internet access, you will get an error when
you open the browser window, but create the project anyway for the practice.

Designed by (Shahzaib Sharif)Shahzaib Sharif Copyright © 2015-2016

Vbdunya2013. Powered by Blogger.