|
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
Part Five ActiveX
The Visual Basic 5 Professional and Enterprise editions introduced ActiveX to Visual Basic programming. ActiveX means different things to different people, but, in general, ActiveX is a specification detailing how objects should interact with each other.
One way the ActiveX specification can be used is to define controls made up of other controls. This kind of ActiveX control can be compiled into an .
OCX file and distributed like any other custom control. An ActiveX control can even be loaded and used by earlier versions of Visual Basic.
Chapter 14 describes an alarm control that makes it easy for a program to wait for a specified time. The AppointmentBook application described in Chapter 3, for example, uses a standard timer control to wait for isolated events far in the future. The alarm control makes this sort of waiting easier.
The bar gauge control described in Chapter 15 allows the user to view and select values graphically. This chapter explains techniques required by any control that has a visible user interface.
Chapter 16 uses an array of bar gauge controls to build a histogram control. This control provides its own scroll bars when needed so the user can view a large amount of data graphically.
The Visual Basic 5 distribution CD contains several documents that describe ActiveX. The Tools\Docs\Ctg subdirectory of the CD contains more than two dozen files that make up the Component Tools Guide. The file CTCMP09.DOC contains the document Building ActiveX Controls. You may want to read these documents, either before or after you read the following chapters, to learn more about building ActiveX controls.
Chapter 14 The Alarm Control
This chapter explains how to use ActiveX to build an alarm control. In some ways the alarm control is similar to the timer control provided by Visual Basic. Neither interacts directly with the end user, both are invisible at run time, and both provide a time-dependent event.
The timer control is designed to schedule periodic tasks that happen fairly often. Such events include displaying the next frame in an animation series and reversing a labels foreground and background colors to make it blink.
In contrast, the alarm control is designed for scheduling isolated events that occur infrequently. For example, the AppointmentBook application described in Chapter 3 schedules events far in advance. AppointmentBook uses a timer to schedule appointment notification, but the timer event is not designed for this task. The alarm control is much better suited for this kind of event scheduling.
The following section, Using the Alarm Control, explains how a program can use the alarm control to schedule events. It also explains the test program AlarmTst.
The Key Techniques section lists and briefly describes the main concepts demonstrated by the alarm control. The rest of this chapter describes these concepts in detail.
Using the Alarm Control
As far as a client application is concerned, an ActiveX control is just like any other control supplied with Visual Basic or purchased from a third party. It has properties, methods, and events similar to those provided by any other control.
ActiveX controls come with several standard properties that require little or no effort on the control designers part. The alarm controls Name, Enabled, Index, Left, Tag, and Top properties require no extra programming to implement.
In addition to these standard properties, the alarm control adds a single new property: AlarmDate. An application should set AlarmDate to the date and time at which the control should generate an Alarm event. If the program specifies a time but not a date, the alarm control assumes the current date. The client application should place whatever code it wants to execute in the alarm controls Alarm event handler.
The example program AlarmTst, which tests the alarm control, is shown in Figure 14.1.
When the user presses one of the buttons, the program sets the alarm controls AlarmDate property to an appropriate value. The button labeled Now + 5 seconds uses the following statement to set AlarmDate for five seconds after the current date and time:
Alarm1.AlarmDate = DateAdd("s", 5, Now)
The two Time + buttons add appropriate amounts to the current time but do not specify a date. For example, the Time + 65 seconds button sets AlarmDate with the following code:
Alarm1.AlarmDate = DateAdd("s", 65, Time)
After setting the alarm controls AlarmDate property, the program displays the current time and the time at which the alarm is scheduled. It then sets the alarms Enabled property to true to activate the control. When the programs Alarm event handler triggers, it presents a message box telling how much time elapsed.
Key Techniques
The alarm control is relatively simple. It does not interact directly with the end user, has no public methods, provides only one event, and has only one nonstandard property. Even such a simple control, however, must handle several important tasks. These tasks are outlined in the following list and described more thoroughly in the rest of the chapter:
- Working with Control Projects. Creating an ActiveX control project is slightly different from creating other projects. This section describes the controls test application and how it interacts with the control.
- Managing Control Display. Controls such as the alarm control do not need to display a visible component at run time. This section explains how the control makes itself invisible at run time and how it prevents the developer from resizing the control at design time.
FIGURE 14.1 The AlarmTst application.
- Implementing Property Procedures. Property procedures allow the client application to read and set property values, both at design time and at run time. This section describes the single property procedure provided by the alarm control.
- Setting Default Property Values. When a control is first created, its properties should have some default values. This section tells how the alarm control sets default property values.
- Reading and Writing Property Values. Controls are created and destroyed many times during design and run time. This section explains how the alarm control saves and restores its property values when it is destroyed and recreated.
- Delegating Standard Properties. The UserControl object defines many standard properties. This section shows how the alarm control uses the UserControl object to simplify management of these properties.
- Raising Events. When the alarm controls AlarmDate arrives, the control generates an Alarm event. This section tells how the control declares and raises this event.
Working with Control Projects
Creating and testing an ActiveX control is a little different from creating other kinds of Visual Basic projects. This section describes some of the development environment features needed to build controls in Visual Basic. It gives a brief tour of the design environment for the alarm project group. Details on what the pieces of the projects do are presented in later sections.
The AlarmTst.VBG project in the Ch13 directory on the compact disk contains the alarm control project group. This group contains two projects: AlarmControl and AlarmTest. The AlarmControl project defines the ActiveX alarm control, and AlarmTest is its test application. The two projects are contained in the same group so they can be easily run together. Keeping them together makes it easy to change the alarm control and then run the test application.
The Project Explorer window shows a hierarchical list of the objects that make up each project. Clicking on a plus symbol in the Project Explorer expands the project hierarchies. The Project Explorer for AlarmTst.VBG is shown in Figure 14.2.
|