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


The AlarmControl project contains one object: Alarm. This is the object that defines the alarm control. The AlarmTst project contains the object AlarmTstForm. This is the main form for the test application.

The Test Application

Double-clicking on the AlarmTstForm entry presents the test program’s main form. This form is shown in design mode in Figure 14.3. The form contains several command buttons and labels, plus a small control that looks like an alarm clock; that control is an alarm control. It behaves much as other controls do. The developer can grab it and drag it around, use the Properties window to examine and modify its properties, and double-click on it to see the source code associated with its event handlers.

Figure 14.4 shows the development environment’s toolbox. At the bottom is the alarm control symbol; this tool allows the developer to create new alarm controls. Double-clicking on the tool creates an alarm control in a default position. Alternatively, the developer can click on the tool, then click and drag on the form to position the new alarm control.


FIGURE 14.2  The Project Explorer.

The ActiveX Control

Double-clicking on the Alarm entry in the Project Explorer displays the control’s object window. This window is very similar to a form window. It represents the UserControl object that contains the ActiveX control’s components. The alarm control’s UserControl object window is shown in Figure 14.5.

While this window is selected, the Properties window displays the UserControl object’s properties. In this case, the UserControl’s Picture property is set to an image of a small alarm clock. This makes the control display the picture when it is placed on a form.

Two other interesting properties for this control are InvisibleAtRuntime and ToolboxBitmap. InvisibleAtRuntime is set to true, indicating the alarm control should not be visible to the end user. The ToolboxBitmap property determines the appearance of the control’s tool in the toolbox.


FIGURE 14.3  The alarm control test form in design mode.


FIGURE 14.4  The development environment’s toolbox with the alarm control tool.

The control’s object window, shown in Figure 14.5, contains a Visual Basic timer control. The alarm control uses the timer to wait until its specified alarm time. Using the toolbox, a control developer can add other controls to the object window. As usual, double-clicking on one of the controls displays its related source code.

While its object window is visible, the alarm control’s tool in the toolbox is disabled. On the test application the alarm control is hatched over with lines. This indicates that the environment cannot use any of the ActiveX control’s customized code that may be required at design time. This code is unavailable whenever the control’s object window is open. Figure 14.6 shows the toolbox, test application, and control’s object window.


FIGURE 14.5  The alarm control’s UserControl object window.


FIGURE 14.6  The toolbox and test application while the control’s object window is visible.

The control can still be dragged to a new position in the test application because that is a standard operation defined for all controls. The control’s standard properties, Name, Index, Left, Tag, and Top, can also be viewed and modified. The two other properties, AlarmDate and Enabled, require specialized control code that cannot execute while the object window is open, so those properties are not displayed in the Properties window.

When the control’s object window is closed, the alarm tool is enabled in the toolbox, and the hatching is removed from the alarm control in the test application’s main form. The AlarmDate and Enabled properties also reappear in the Properties window.

Running the Test Application

Pressing F5 or selecting the Start command from the Run menu starts the test application. If the ActiveX control’s object window is open, the environment automatically closes it so that it can use the control’s code.

Managing Control Display

Controls such as the timer and alarm controls do not need to display a visible component at run time. Whether an ActiveX control is visible at run time is controlled by the UserControl’s InvisibleAtRuntime property. If this property is true, it will not be visible to the user at run time.

Because these controls are invisible to the user, there is no reason for the application designer to change their sizes. The alarm control’s UserControl_Resize event handler triggers whenever the designer changes the control’s size. The following code uses the UserControl’s Size method to set the UserControl’s width and height back to their proper values. Note that the width and height parameters passed to Size should be specified in twips. The Size routine uses twips no matter what ScaleMode is used by the UserControl object or the object containing the control in the client application.

Private Sub UserControl_Resize()
    Size 320, 320
End Sub

Implementing Property Procedures

An ActiveX control provides access to its properties through public property procedures. The values represented by the properties are generally stored either in variables local to the control or in the properties of controls contained within the ActiveX control.

To allow a developer or the client application to read a property, the control should provide a property get procedure. To allow a developer or application to modify a property, the control should provide a property let or property set procedure. Sometimes a control will not provide both. For example, a control can provide a read-only property by providing a property get procedure with no corresponding property let procedure.

The alarm control stores the time at which it should generate an alarm in the private variable alarm_date. The property get and let procedures named AlarmDate give developers and client applications indirect access to this value.

The AlarmDate property get procedure shown in the following code simply returns the value of alarm_date.

Property Get AlarmDate() As Date
    AlarmDate = alarm_date
End Property

The AlarmDate property let procedure that follows is a bit more complicated. First, the procedure examines the value it has been passed by the client application to see if the value includes a date (2/7/97 1:00 PM) or if it is just a time (1:00 PM). If the value is only a time, the procedure adds the current date.

Next, the procedure checks to see if the alarm date is more than 10 years from the current date. This prevents overflow problems later when the code calculates the number of seconds between the current time and the alarm date.

AlarmDate then stores the new value of alarm_date and calls the PropertyChanged subroutine. This notifies the Visual Basic environment that the value of the AlarmDate property has been changed and that the corresponding value in the Properties window must be updated. It also lets the environment know that the new value will need to be saved later in the test application’s form file. This process is described in more detail in “Reading and Writing Property Values.”


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.