Click Here!



home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


Introduction

In recent years Visual Basic has revolutionized Windows programming. By handling many of the tedious details of Windows programming, Visual Basic allows developers to focus on more application-specific tasks.

Despite the fact that Visual Basic handles the more mundane Windows programming details, few books cover sophisticated programming methods. Books that do mention advanced concepts do so only briefly, and they often demonstrate complicated techniques with over-simplified examples. For instance, the “Scribble” application for drawing a simple freehand picture using the mouse is often used to demonstrate such concepts as SDI and MDI interfaces, saving and loading data in files, common dialogs, drawing attributes, and even object-oriented data structures. The Scribble application may teach important lessons, but it provides little help in solving real-world problems because very few real applications call for freehand drawing. The reader is left to decide how to apply the lessons learned to solve real programming problems.

By providing complete solutions to realistic problems, Advanced Visual Basic Techniques goes farther than other Visual Basic texts. A book can stretch artificial examples like Scribble only so far. It would be difficult to extend Scribble to reasonably demonstrate client/server architectures, storage of bitmaps for database applications, and Web browsing. By giving separate solutions to a variety of realistic problems, Advanced Visual Basic Techniques can explain all of these techniques within meaningful contexts.

Advanced Visual Basic Techniques bridges the gap between programming technique and real-world applications. It provides working solutions to a selection of realistic problems that you might actually want to solve, and, in the course of explaining the solutions, it demonstrates advanced programming concepts that you will find in few other Visual Basic programming books.

What You Will Gain

This book and its compact disk will provide you with the following:

•  An in-depth introduction to advanced application programming techniques. After reading the book and studying the applications, you will be able to incorporate sophisticated capabilities into your Visual Basic projects.
•  Complete, ready-to-run applications that solve real-world problems. You can use these applications as they are written, or you can customize them to meet your specific needs.

If you are not already an experienced Visual Basic application developer, you will be one by the time you have studied all the sample applications. Armed with an arsenal of powerful implementation techniques, you will be prepared to attack the most complex development problems.

Intended Audience

This book covers intermediate and advanced Visual Basic application programming topics. It does not teach the Visual Basic language itself. If you have a good understanding of the fundamentals of Visual Basic, you are ready for the applications and advanced techniques in this book. If you do not know how to place controls on a form, write an event handler to respond to a command button’s Click event, or run a Visual Basic program, you might want to brush up on the basics a bit before proceeding.

Even if you have not yet completely mastered Visual Basic, you will be able to understand and run the applications. With occasional references to your Visual Basic manuals, you will be able to modify the examples to build similar applications of your own. By the time you have finished exploring all of the examples, you will have become an experienced Visual Basic application developer.

How to Use This Book

You might take a couple of different approaches to using this book. Naturally, you can read the book from front to back, studying each chapter in turn. This will give you a broad introduction to many powerful application implementation techniques. You will then be able to incorporate those techniques into your own Visual Basic applications.

A second approach is to pick a chapter that describes an application similar to one you want to build. Then you can study that chapter and modify the application to suit your particular needs. If you want to conduct a survey using a Web page, for example, you could move immediately to Chapter 18. The WebSurvey application described there provides a framework you can use to conduct your survey. With a few modifications you will be running your survey in no time.

The later section “How this Book Is Organized” describes each of the sample applications so that you can find the one that best fits your particular needs. Each chapter begins with a description of the application explained in that chapter. Each also includes a “Key Techniques” section listing the main concepts and programming methods described in that chapter.

A third strategy for using Advanced Visual Basic Techniques is to select a technique or concept you would like to use in your applications. You can then learn more about that technique by studying the corresponding sample applications. For example, you might want to add a recent file list to your drawing application. The ExpenseReporter application described in Chapters 1 and 2 uses a recent file list. By reading the appropriate sections in Chapter 1, you can learn how to add this feature to your application.

Finally, each of the applications described in this book was selected for its easy usability. You will probably find at least one or two that you can use as they are written. For example, the PropertySetter, Aligner, Scroller, and AddInMaker applications described in Chapters 4 through 7 are handy tools that make Visual Basic programming easier. You can add these applications to the Add-Ins menu of your Visual Basic development environment and take advantage of their power even if you do not examine their code in detail.

How This Book Is Organized

The chapters presented later in the book are generally more “advanced” than those at the beginning. You may think the earlier chapters are less “state-of-the-art,” but they are not necessarily less complicated or less difficult to understand. For example, Chapters 1 and 2 describe a single-document interface application. Putting all of the finishing touches on even a simple application can be quite difficult. It is no trivial task to give this application recent file lists, help, context-sensitive help, scrolling regions, printing, printer control, print previewing, field validations, and all of the other features needed by a professional-quality application.

The chapters are arranged so that they do not depend on your having read the earlier material. This means you can read the chapters in any order you like. The exception to this rule is Chapter 2, which continues the discussion of the ExpenseReporter application described in Chapter 1. Chapter 1 explains ExpenseReporter from the user’s point of view. Although the concepts explained in Chapter 2 do not require that you understand all of Chapter 1, they will make more sense if you have read at least the beginning of Chapter 1.

Applications later in the book may use techniques described earlier without any explanation. For example, several applications provide some form of help, but help systems are described only in the first few chapters. When you are working with an application, if you come across a topic that was covered earlier, you can always go back to the previous chapters to learn more.

The chapters in Advanced Visual Basic Techniques are divided into five parts.

Part I—Application Basics

The applications described in these chapters cover basic techniques that are common in many Visual Basic applications. Some concepts, such as help and context-sensitive help, are essential for any successful application. Others, including object-oriented input and output, are useful in a wide variety of circumstances.

•  Chapter 1: ExpenseReporter—Part I. ExpenseReporter is a single-document interface (SDI) program that allows the user to create, edit, and print a one-page expense report. Chapter 1 explains application fundamentals including scrolling areas, control arrangement, dynamic controls, field navigation, SDI issues, loading and saving files, menu design, and recent file lists.
•  Chapter 2: ExpenseReporter—Part II. Chapter 2 continues the explanation of ExpenseReporter started in Chapter 1. It covers the more complicated topics of field- and form-level validation, printing, print preview, help, about dialogs, and splash screens.
•  Chapter 3: AppointmentBook. This application allows the user to schedule and track appointments. When it is time for an appointment, the program presents a reminder. AppointmentBook shows how to work with bit masks, use the MVC paradigm to manage complex views of data, create rotated fonts, scroll large amounts of data, set alarms to wait until a specified time, and create nonrectangular forms.

Part II—Programming Tools

These applications implement useful programming tools. You can add them to your Visual Basic development environment’s Add-Ins menu and use them to make routine programming tasks easier. In addition to providing you with helpful tools, these applications show how you can create other programming aids to automate mundane programming chores.

•  Chapter 4: PropertySetter. This application allows a developer to view and modify the properties of many controls at the same time. For example, if a group of label controls is selected, PropertySetter can give them all the same name and place them in a control array. The chapter explains how to create, connect, and disconnect add-ins, access controls selected in the development environment, and set control properties.
•  Chapter 5: Aligner. This add-in allows a developer to align controls on a Visual Basic form easily. Aligner can arrange controls in several ways including vertically, horizontally, and in grids. Aligner demonstrates how an add-in can reposition existing controls in a Visual Basic project.
•  Chapter 6: Scroller. The Scroller add-in takes a group of selected controls and places them in a scrolling region. It shows how an add-in can create new controls, add them to a form, and place one control inside another. It also demonstrates how to create add-in submenus and how to add Visual Basic source code to a form.
•  Chapter 7: AddInMaker. AddInMaker is an add-in that automates the creation of add-ins. It provides an interesting exercise in add-in creation. Once you understand an add-in that creates other add-ins, you will understand the topic thoroughly.

Part III—Databasics

Part III contains database applications that run on the same computer that holds the database. Using a database, your applications can easily manage large amounts of complex data. A database also allows multiple applications to use the same data in different ways without interfering with each other.

•  Chapter 8: PeopleWatcher. This application is a corporate personnel system. It demonstrates how to use an outline control to display data, how to store bitmaps in an Access database, and how to manage user changes to data. It also shows how to present different users with different views of the data and how to determine which users should be allowed to modify specific data fields.
•  Chapter 9: Query. The Query application allows the user to enter and execute SQL queries on a database. This chapter explains how an application can create, connect, and disconnect databases and process SQL statements. It also explains the most common SQL commands including SELECT, INSERT, UPDATE, and DELETE.
•  Chapter 10: PeopleWatcher Remote. This application extends the PeopleWatcher application described in Chapter 8 across a network. By connecting the program to a database located elsewhere in the network, the application allows multiple users on different computers to use PeopleWatcher at the same time. This chapter explains how a program can connect directly to a database across a network and how it can use local tables attached to tables in a remote database.

Part IV—Client/Server Applications

Part IV includes client/server applications. In a client/server application, one program, the server, provides some sort of service for another application, the client. Client/server applications allow a system to distribute functionality across a network to provide features such as improved performance and centralized resource management.

•  Chapter 11: TimeSync. TimeSync allows two computers to synchronize their clocks. This application has been kept fairly simple so that you can concentrate on the process of creating a client/server application rather than on application issues. This chapter describes the complicated process of creating, testing, and installing a simple client/server application.
•  Chapter 12: QueryServer. This application uses client and server programs to generate standard reports. It shows how a centralized report server can make managing reports distributed across a network simple while minimizing network traffic.
•  Chapter 13: AsyncServer. Normally a client application waits until a server finishes its processing tasks before continuing. AsyncServer shows how a client can continue processing while one or more servers run asynchronously. This allows the application to take full advantage of the network’s distributed processing power.

Part V—ActiveX

ActiveX is a specification that describes how objects should interact with each other. Because Visual Basic 5 complies with the ActiveX standard, it allows a programmer to build new objects that were not possible before. These objects include controls created from other controls. The chapters in Part V explain how to create ActiveX controls that can be used in Visual Basic projects or applications written in other languages.

•  Chapter 14: The Alarm Control. Visual Basic’s Timer control is designed for triggering frequently occurring events. In contrast, the Alarm control schedules isolated events that occur relatively far in the future. This chapter shows how to create properties and events; initialize, save, and restore property values; delegate responsibilities to other controls; and use an ActiveX control in another application.
•  Chapter 15: The Bar Gauge Control. The Bar Gauge control uses several techniques that are not needed by the Alarm control. The Bar Gauge demonstrates how to draw a control with a visible interface, manage mouse movement, and handle keyboard events.
•  Chapter 16: The Histogram Control. ActiveX controls behave almost exactly as other controls behave. In particular, they can be used to create other ActiveX controls. The Histogram control uses a set of Bar Gauge controls to display a set of data values. This chapter shows how an ActiveX control can manage an array of other ActiveX controls, use a scroll bar to allow the user to scroll through data, support indexed properties, and raise indexed events.

Part VI—Spinning the Web

The World Wide Web allows millions of computer users to visit a multitude of different computers effortlessly. On those computers, visitors can find a practically limitless number of pages of multimedia information. The applications presented in Part VI deal with creating and manipulating Web pages in Visual Basic.

•  Chapter 17: ActiveX Controls on the Web. This chapter explains how a Web page can display ActiveX controls. It describes special Web licensing issues, ensuring control safety, and the actual use of ActiveX controls in a Web page. It presents several examples including a WebLabel control that can be used to display and update labels on a Web page. It also explains how a control can load larger amounts of data, such as pictures, asynchronously from the server computer.
•  Chapter 18: WebSurvey. This application allows a Web user to fill in a survey form and send the results to a remote program written in Visual Basic. That program can then process the results in any way necessary—for example, saving the results in a file. This chapter explains how to build a survey Web page, send data across the Web to a remote program, receive data at the remote program, and pack and unpack data so that it is not damaged during the process.
•  Chapter 19: SiteMapper. The SiteMapper application starts at a Web address and visits all of the Web documents it can reach without leaving the original site. It shows how the various documents are related, and it lists the references to image files they contain. SiteMapper shows how a program can retrieve Web documents, parse URLs, expand partial URLs, and follow links to visit a Web site.

Approach

Most Windows programs are event-driven. The computer spends most of its time sitting around waiting for the user to do something that will send the program into action. When the user presses a command button, selects a menu item, or adjusts the value of a scroll bar, the program briefly awakens. It performs some task and then lapses back into hibernation to await the user’s next command.

This sort of design makes an application responsive to the user’s needs, but it can make the application design harder to explain. Simple diagrams such as flow charts cannot adequately represent the program’s architecture. Because the application spends most of its time idle, there is little control flow to diagram. Other constructions, such as entity-relationship (ER) diagrams, can describe relationships among classes and certain other large-scale objects, but they do not explain how event-driven programs work.

It is probably more productive to think of a typical Windows program as a collection of user interface elements tied to underlying code. A form contains command button, menu, and list box controls. There is often little or no meaningful relationship among these controls. It makes no sense to think of one command button as a “child” of another. It also makes little sense to think of a menu item “passing control” to a list box. Generally, the user interacts with a control, and that control invokes some Visual Basic source code. It is the controls themselves and the underlying code that define the application’s architecture.

The chapters that follow focus on the code behind the controls. Each chapter begins by explaining what its application does from the user’s point of view. It then gives a list of the key techniques demonstrated by the underlying code behind the controls. The sections that follow describe those key concepts.

Note that the concepts do not always match one-to-one with the controls. For example, the ExpenseReporter application described in Chapter 1 contains many fields that contain numbers. This application uses field validation functions to ensure that the user enters only valid numeric values in those fields. In this case, a single set of field validation routines can manage user input for dozens of different text boxes.

An application’s controls function independently, but the underlying key concepts are often coupled. For example, the functions that implement recent file lists are closely related to the subroutines that load, save, and create new files. Because there is no clear flow of control among these subroutines, you may need to read about them all to get a clear understanding of how they fit together.

This book describes only pieces of code that are particularly interesting or confusing. Control placement and source code that is straightforward are not described here. You can find the complete source code for every application on the accompanying compact disk. See the section “Using the Compact Disk” for instructions on loading the source code from the disk.

Equipment You Will Need

To run and modify the example applications, you need a computer that is reasonably able to run Visual Basic 5.0. A 486-based computer running Microsoft Windows 95 or Windows NT will work. You will also need a compact disk drive to load the programs from the accompanying compact disk.

The client/server applications described in Chapters 10 through 13 use more advanced networking and OLE server techniques that work only in the Visual Basic Enterprise Edition under Windows 95 and Windows NT. The ActiveX applications presented in Chapters 14 through 19 use features introduced in Visual Basic 5.

If you do not have the Enterprise Edition, or if you are running an older version of Windows, some of these applications will not run as they are presented in the book. You can still read the chapters and learn valuable lessons about the application architectures. Using the fundamental architectures and some file management tricks, you can even implement similar applications without using OLE servers. This is a hard route to follow, however, and you may be better off if you upgrade your software and save yourself a lot of time and trouble.

All the applications on the CD, except the programs in Chapter 13, have been tested in Visual Basic 5.0 under Windows 95 and Windows NT. The programs explained in Chapters 1, 2, and 4 through 13 were also tested using 32-bit Visual Basic 4.0. Though the programs may look slightly different in the two environments, they will perform in roughly the same manner. The applications described in Chapters 3 and 14 through 19 use features introduced by Visual Basic 5.0 so they will not run with earlier versions of Visual Basic.

The applications will run at different speeds on different computers with different configurations, but they will all work. If you own a 200 megahertz Pentium with 64MB of memory, applications will run much faster than they would if you owned a 486-based computer with 8MB of memory. Both machines will be able to run the applications, but at different speeds. You will quickly learn the limits of your hardware.

Using the Compact Disk

The accompanying compact disk contains Visual Basic source code for all the applications described in the book. The files on the compact disk are separated by chapter. Code for the PeopleWatcher application described in Chapter 8, for example, is stored in the Ch8 subdirectory. Some chapter directories contain subdirectories that hold data or different programs described within the chapter.

You can load the example programs into the Visual Basic development environment using the Open Project command in the File menu. You can select the files directly from the compact disk, or you can copy them onto your hard disk first. Note that files on a compact disk are always marked read-only because you cannot save files to a compact disk. If you copy files onto your hard disk, the copies are also marked as read-only. If you want to modify these files, you must give yourself write permission for them: this is particularly important for database files because the database programs will not run if their databases cannot be accessed.

You can do this with the Windows Explorer. First, copy the files you want onto your hard disk. Then select the files and invoke the Properties command in the Explorer’s File menu. Uncheck the Read Only check box and press the OK button. At this point, you can make changes to the copied application and save the changes to your hard disk. Do not worry about making careless changes to the copy and accidentally breaking the application. You can always restore the original versions from the compact disk.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.