Chapter 1: Introduction to Application Development Using Visual Basic



Chapter 1: Introduction to Application Development Using Visual Basic

In this chapter, you will learn about the Microsoft Visual Basic development environment. Visual Basic is a powerful development tool that you can use to quickly and easily create feature-rich applications for Microsoft Windows and Windows NT operating systems. Although professional programmers use Visual Basic, it is easy for novices to program in Visual Basic with professional results.

Based on the Basic programming language, Visual Basic differs from previous versions of Basic because it is based on an event-driven programming model. Visual Basic provides a rapid application development (RAD) environment, a rich object-based language, and an easy-to-use set of debugging tools.

This chapter introduces you to the Visual Basic environment and explains some of the concepts that you need to understand to create programs for the Windows operating system.

Objectives

After completing this chapter, you will be able to:

  • Identify the elements in the Visual Basic development environment.

  • Explain the difference between design time and run time.

  • Explain the concept of event-driven programming.

  • Describe the purpose of a project file.

  • List the file types that can be included in a project.

Features of Visual Basic

Using Visual Basic is the quickest and easiest way to create powerful, full-featured applications that take advantage of the graphical user interface in Windows. For companies developing custom applications, Visual Basic reduces development time and costs. Its intuitive interface makes Visual Basic an excellent tool for programmers.

New users benefit from mouse operations and a consistent look and feel; more advanced users benefit from easy-to-use features, such as drop-down list boxes and multiple-window applications.

Visual Basic provides the basis for the programming language that is used in all Microsoft Office applications, Visual Basic for Applications, and a variety of applications from other vendors.

Visual Basic supports a number of features that make it an excellent language for quickly creating full-featured solutions, including the following:

  • Data access features

    By using data access features, you can create databases, front-end applications, and scalable server-side components for most database formats, including Microsoft SQL Server and other enterprise-level databases.

  • ActiveX technologies

    With ActiveX technologies, you can use the functionality provided by other applications, such as the Word processor, the Microsoft Excel spreadsheet, and other Windows applications. You can even automate applications and objects that were created by using the Professional or Enterprise editions of Visual Basic.

  • Internet capabilities

    Internet capabilities make it easy to provide access to documents and applications across the Internet, or an intranet, from within your application, or to create Internet server applications.

  • Rapid application development (RAD)

  • Support for multilingual applications

  • Interactive debugging support

Editions of Visual Basic

In this section, you will learn about the different editions of Visual Basic. The Visual Basic development system version 6.0 is available in the following three editions:

  • Learning Edition

    The Learning Edition allows programmers to easily create powerful applications for the Windows and Windows NT operating systems.

  • Professional Edition

    The Professional Edition adds to the capabilities of the Learning Edition by allowing you to create client/server or Internet-enabled applications.

  • Enterprise Edition

    Developers in a corporate environment benefit by using the advanced features in the Enterprise Edition to create robust distributed applications in a team setting.

    This course is designed for programmers using the Professional or Enterprise Editions of Visual Basic.

Learning Edition

You can use the Learning Edition of Visual Basic to create 32-bit programs. The Learning Edition includes the following:

  • The Visual Basic development environment

  • Standard controls

  • Samples

  • Icons

  • The Package & Deployment Wizard

  • ActiveX Data Objects (ADO)

  • ADO Data control

    The ADO Data control is a new OLEDB-aware data source control that functions much like the intrinsic Data and Remote Data controls in that it allows you to create a database application with a minimum amount of code.

  • The Learn VB Now multimedia course

  • Microsoft Developer Network (MSDN) CDs containing full online documentation

Professional Edition

The Professional Edition of Visual Basic has all of the features that are found in the Learning Edition (except the Learn VB Now multimedia course), and also includes the following:

  • Additional ActiveX controls

  • Internet Information Server Application Designer

  • Data Report Designer

  • Dynamic HTML Page Designer

  • Integrated Data Tools and Data Environment

  • Visual Studio Professional Features book

Enterprise Edition

The Enterprise Edition includes all the features found in the Professional Edition, and also includes the following:

  • Microsoft Visual SourceSafe

  • Microsoft Internet Information Server

  • Microsoft Transaction Server

  • SQL Editor

    The SQL Editor allows you to create and edit stored procedures and triggers in both SQL Server and Oracle from within the Visual Basic development environment.

  • Application Performance Explorer

  • Visual Component Manager

Visual Basic Terminology

As with any programming language, using Visual Basic requires an understanding of some common terminology. The following table lists some key terms used in Visual Basic. You’ll learn more about each term later in the course.

Term Definition
Design timeAny time an application is being developed in the Visual Basic environment.
Run timeAny time an application is running. At run time, the programmer interacts with the application as the user would.
FormsWindows that can be customized to serve as the interface for an application or as dialog boxes used to gather information from the user.
ControlsGraphic representations of objects, such as buttons, list boxes, and edit boxes, that users manipulate to provide information to the application.
ObjectsA general term used to describe all the forms and controls that make up a program.
PropertiesThe characteristics of an object, such as size, caption, or color.
MethodsThe actions that an object can perform or that can be performed on the object.
EventsActions recognized by a form or control. Events occur as the user, operating system, or application interacts with the objects of a program.
Event-driven programmingWhen a program is event-driven, its code executes in response to events invoked by the user, operating system, or application. This differs from procedural programming, in which the program starts at the first line of code and follows a defined path, calling procedures as needed.

Working in the Development Environment

In this section, you will learn about the different elements of the Visual Basic development environment. When you start Visual Basic and select a project type, the graphical development environment appears. The following illustration shows some of the development environment’s elements.

Click to view graphic
Click to view graphic

Visual Basic Project Types

Visual Basic offers several project templates that are designed to support the development of different kinds of applications and components. As you begin developing an application, you must first decide what kind of project template to use. A project template contains the basic project objects and environment settings that you need to create the type of application or component that you want to build.

This course discusses only the Standard EXE project. For more information on using the other projects in the following list, please see Mastering Microsoft Visual Basic 6 Development.

To create a new project

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Select the project template that you want, and then click OK.

The following templates are available by default:

  • Standard EXE

    Standard EXE projects contain a form by default. Use this project template to develop a stand-alone application. This is the project type that you will be using in this course.

  • Data Project

    Use this project template to develop an application that reads or manipulates data from a data source.

  • ActiveX EXE/ActiveX DLL

    Use these project templates to develop COM components that expose functionality to other applications.

    Use an ActiveX EXE project template if your component will both expose functionality programmatically and run as a stand-alone application. Use an ActiveX DLL project template if your component will only be used programmatically by another application.

  • ActiveX Control

    Use this project template to create a component that is designed to be a user interface element in a form or dialog box.

  • ActiveX Document EXE/ActiveX Document DLL

    Use these project templates to create components that are designed for use in a document object container, such as Microsoft Internet Explorer.

  • DHTML Application

    Use this project template to create a component that can be used on the client side of a Web application.

  • IIS Application

    Use this project template to create a component that can be used on the server side of a Web application.

Toolbox

The toolbox contains the objects and controls that you can add to forms to create the user interface of your applications. You can add additional controls to the toolbox by using the Components command on the Project menu. In addition, you can right-click anywhere in the toolbox and select Components from the pop-up menu.

The following illustration shows the toolbox.

Click to view graphic
Click to view graphic

Form Designer

In Visual Basic, a form is a window used in your application. For each form in your application, Visual Basic provides a Form Designer window at design time that contains the form and all of the controls that you place on the form.

The following illustration shows the Form Designer window and the default form. The default form contains the minimum elements used by most forms: a title bar, control box, and Minimize, Maximize, and Close buttons.

Form Designer window

To open the Form Designer window

  1. In the Project Explorer window, click the form that you want to view.

  2. Click the View Object button.

    -or–

    On the Visual Basic View menu, click Object.

Project Explorer Window

The Project Explorer window lists the collection of files used to build an application. The collection of files is called a project. You can switch between the Form and Code views by using the View Form and View Code buttons.

The following illustration shows the Project Explorer window.

Project Explorer window

Properties Window

The Properties window lists the property settings for the selected form or control that can be modified while the program is being edited. (Other property settings can be modified only at run time.) A property describes a characteristic of an object, such as size, caption, or color.

You can view properties alphabetically or by category by clicking the Alphabetic or Categorized tabs in the Properties window. When you click a property, a short description of the property appears.

The following illustration shows the Properties window.

Properties window

Code Editor Window

You write the code statements for a project in the Code Editor window (also known as the Code window). Visual Basic code can be either associated with a form in your project or contained in a separate code module. A separate Code Editor window is displayed for each form or module in your project, making it easy to organize, view, and navigate through the code.

The Code Editor contains two drop-down lists at the top of the window: the Object list and the Procedure list. The Object list contains a list of all the controls contained on the form. If you select a control name from the list, the Procedure list shows all of the events for that control (or actions that the control can perform and that can be interpreted by your application). By using the Object and Procedure lists together, you can quickly locate and edit the code in your application.

The following illustration shows the Code Editor window.

Code Editor window

To open the Code Editor window

  1. In the Project Explorer window, click the form whose code you want to view.

  2. Click the View Code button.

    -or-

    On the Visual Basic View menu, click Code.

Standard Toolbar

The Standard toolbar includes buttons for many of the most common commands used in Visual Basic, such as Open Project, Save Project, Start, Break, and End. This toolbar also contains buttons that display the Project Explorer, the Properties window, the toolbox, and other elements in the Visual Basic development environment.

The following illustration shows the Standard toolbar.

Click to view graphic
Click to view graphic

Toolbars can either be docked (attached) or floating. By default, most toolbars are docked; however, you can undock or dock a toolbar at any time. To undock a toolbar, click near the double bar along the left side of a toolbar, and drag the toolbar away from its docked position. To dock a toolbar, drag it to an edge of the main window.

Event-Driven Programming

Applications written in Visual Basic are event-driven. Event-driven programming can best be understood by contrasting it with procedural programming.

Procedural Programming vs. Event-Driven Programming

Applications written in procedural languages execute by proceeding logically through the program code, one line at a time. Logic flow can be temporarily transferred to other parts of the program through the GoTo, GoSub, and Call statements, directing the program from beginning to end.

In contrast, program statements in an event-driven application execute only when a specific event calls a section of code that is assigned to that event. Events can be triggered by keyboard input, mouse actions, the operating system, or code in the application. For example, consider what happens when the user clicks a command button named Command1 on a form. The mouse click is an event. When the Click event occurs, Visual Basic executes the code in the Sub procedure named Command1_Click. When the code has finished running, Visual Basic waits for the next event.

The following illustration contrasts procedural and event-driven programming.

Click to view graphic
Click to view graphic

Event-Driven Programming in Visual Basic

To better understand how event-driven programming works in Visual Basic, consider the following example application. The application consists of a single form named Simple Controls that contains four objects: two command buttons, a label, and a text box. One button draws a border around the label. The other button resets the value in the text box to its initial value.

The following illustration shows the form that makes up the application’s user interface.

Click to view graphic
Click to view graphic

To see the demonstration “Understanding Event-Driven Programming,” see the accompanying CD-ROM.

This application will also draw a border around the label when the user clicks a button. To add this functionality, you must write code to respond to the Click event for the Add Border to Label button.

Click to view graphic
Click to view graphic

To understand the code, you must know that the name of the Add Border to Label button is cmdBorder. The button’s name is not the same as the text that appears on the button. (You’ll learn more about this in “Setting Control Properties” on page 42 in Chapter 2, “Visual Basic Fundamentals.”) The name of the procedure, cmdBorder_Click, denotes that the procedure responds to the Click event for the cmdBorder command button. Notice that the event is represented by adding an underscore and the name of the event to the name of the control.

The next statement adds the border to the Label control named lblName. BorderStyle is a property of the Label control. The statement changes the property from its initial setting, vbNormal, to a new setting, vbFixedSingle.

The final statement, End Sub, denotes the end of the procedure.


NOTE
The purpose of this example application is to familiarize you with the basic approach used in event-driven programming. If the syntax being used seems unfamiliar, do not be concerned. It’s explained in much more detail later in the course.

Now consider the code associated with the Click event for the cmdReset button. Clicking the button changes the Text property in the txtName text box back to its original value, “John Doe,” as shown in the following example code:

Sub cmdReset_Click()
txtName.Text = "John Doe"
End Sub
As in the previous procedure, the procedure name consists of the object name, an underscore, and the event to which the procedure responds.

The example application consists of two event procedures that contain a total of six lines of code. The event-driven nature of the application handles the logic required to direct program execution to the appropriate task in response to user actions. The programming logic required to implement the same type of application in a procedural language would be much more complicated and would involve many more lines of code.


Next

Visit Microsoft Press for more information on
Microsoft® Mastering: Microsoft Visual Basic® 6.0 Fundamentals



© 2001 Microsoft Corporation. All rights reserved. All use of this site is subject to the Microsoft Press Terms & Conditions, including the Privacy Statement. Microsoft Press, Visual Studio, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.