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


Finally, if the control’s Enabled property currently has the value true, the AlarmDate property procedure invokes the AlarmTimer_Timer event handler. This subroutine starts the timer that controls the alarm.

Property Let AlarmDate(ByVal d As Date)
    ‘  If this is only a time and not a date,
    ‘  add today’s date.
    ‘  When converted into a single, a Date’s
    ‘  date part is to the left of the decimal
    ‘  and the time part is to the right. Thus
    ‘  a time with no date < 1.0.
    If CSng(d) < 1# Then d = d + Date

    ‘  Make sure the alarm is less than 10 years
    ‘  from now.
    If Abs(DateDiff("yyyy", Now, d)) > 10 Then Exit Property
 
    ‘  Set the alarm date.
    alarm_date = d

    PropertyChanged "AlarmDate" ‘ Tell VB.
   
    ‘  If we are enabled, start the timer.
    If Enabled Then AlarmTimer_Timer
End Property

Setting Default Property Values

When an ActiveX control is initially added to a form at design time, its properties are given default values. The control sets these values in the UserControl object’s InitProperties event handler.

Private Const dflt_Enabled = False
    :
Private Sub UserControl_InitProperties()
    ‘  Do not run while loading.
    loading = True
   
    AlarmDate = Now
    Enabled = dflt_Enabled
   
    ‘  Run now if appropriate.
    loading = False
    AlarmTimer_Timer
End Sub

In this case AlarmDate is set to a calculated value: the value returned by Visual Basic’s Now function. This value is unlikely to be useful to the application designer, but it makes more sense than some arbitrary constant value such as Jan 1, 1970, 1:00 PM. At least its date may be correct.

The Enabled property is initially set to the constant value false. The application designer will probably want the alarm control disabled until the program later enables it.

Using Ambient Properties

Another good method for selecting initialization values is to use the ambient properties of the container that holds the ActiveX control. The control can access ambient properties through the UserControl object’s Ambient property. The following code shows how a control could initialize its BackColor property to the ambient BackColor value.

BackColor = Ambient.BackColor

Ambient properties give the control clues about how it can best display itself. They do not necessarily match the properties of the container that holds the control. For example, the ambient BackColor property matches the BackColor property of the form. If the form’s background is blue when the new control is created, the control will have a blue background even if it is placed within a picture box that has a yellow background. If all ActiveX controls use ambient properties, the application should have a consistent appearance.

A control should generally initialize its properties to ambient values if possible. In particular, the control should use ambient values to initialize properties that affect the control’s appearance, such as ForeColor, BackColor, Font, and TextAlign.

Deferring Execution during Initialization

As a control’s InitProperties event handler sets property values, it may temporarily cause the control to activate. For the alarm control InitProperties first sets the value of AlarmDate. If the Enabled property happened to be initialized by Visual Basic with the value true, the AlarmDate property let procedure will activate the alarm control. That routine will see that the alarm date has already arrived so that it will immediately trigger an Alarm event even though the application is probably not ready.

To prevent this from happening, the InitProperties routine sets the variable loading to true. The AlarmTimer_Timer subroutine checks this value to see if the control is in the middle of loading itself. If so, the routine does nothing. When InitProperties is finished loading the control, it sets loading to false and invokes AlarmTimer_Timer directly so it can take action if necessary.

Deferring action is even more important for other kinds of ActiveX controls. The bar gauge controls described in Chapter 15 have many property procedures that force the control to redraw itself. Using the loading variable to defer redrawing until every property is initialized saves many unnecessary redraws.

Reading and Writing Property Values

When a control is first added to a form in design mode, it is created. The UserControl object’s InitProperties event handler runs to set default values for the control’s properties.

If the designer closes a form, the controls on the form are destroyed. At that point, the property values for the controls must be saved so they can be recovered later. ActiveX controls provide a WriteProperties event handler to perform this task.

WriteProperties takes as a parameter a PropertyBag object. Using this object’s WriteProperty subroutine, the control can save its property values into the form’s .FRM file.

WriteProperty takes three parameters. The first is the name of the property. The second is the value the property currently has. The last parameter is a default value. If the property’s current value matches the default value, it is not saved to the file. Later, when the ReadProperties event handler loads values from the file, it sets properties with missing values to their defaults. This arrangement saves space in the form file when many properties have their default values.

The following code shows the alarm control’s WriteProperties event handler. The Enabled property’s default value is false, the value more likely to be specified at design time. The default value for AlarmDate is the rather arbitrary Jan 1, 2000, 12:00 PM. Even though it is extremely unlikely that this value will ever be used by the application designer, the WriteProperty subroutine requires some default value. All that matters is that the default used when writing the property is the same as the value used when reading it (described in the next paragraphs). Then, in the unlikely event that the designer does select this value, the same value will be assigned to AlarmDate when the file is reloaded.

Private Const dflt_AlarmDate = #1/1/2000 12:00:00 PM#
Private Const dflt_Enabled = False
    :
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "Enabled", Enabled, dflt_Enabled
    PropBag.WriteProperty "AlarmDate", AlarmDate, dflt_AlarmDate
End Sub


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.