VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 13

Using the InitProperties Event to Set Default Starting Property Values

The InitProperties event fires just once in the lifetime of a control: When the developer first sites it on its container from the toolbox.

The InitProperties event procedure is therefore the perfect place to set initial default values for your control's properties since you'll want these default values to show up when the developer first sites the control on its container. However, you won't want the default values to override any later changes the developer makes after siting the control.

You will mainly use InitProperties to initiate default values for your custom control's properties. If you've implemented a property as a Public variable, you can simply set the variable name to the desired default value. On the other hand, if you're using Property procedures to implement a property, you can set its default value by writing to the Private memory variable or other holder (such as a constituent control property) that maintains the property's value.

You can set these default properties in a number of ways, depending on the type of property you want to set and on how you've chosen to implement the property:

  • Assign a literal value to the Private storage variable implementing the custom property.

  • Assign a default constant value (as discussed in the previous section) to the Private storage variable.

  • Derive a property's initial value from some Extender property.

  • Derive a property's initial value from some Ambient property.

The InitProperties event procedure shown in Listing 13.6 gives examples of the techniques listed above.

The code in the following listing assumes that Slider and TextBox controls exist in the project with respective names Slider1 and txtDate. It also pre-supposes Private variables and constants that implement property values and their defaults, as shown in the General Declarations section of the listing.


Option Explicit
Private Const defCelsius = 30
Private m_TemperatureDate As Date
Private m_Celsius As Single
Private m_Caption as String

Private Sub UserControl_InitProperties()
   'Set temperaturedate to a value
   'determined right here
   m_TemperatureDate = DateSerial(1997, 1, 1)
   'Set Celsius to a default
   'constant value
   m_Celsius = defCELSIUS
   'Compute Fahrenheit from that
   RecalcFahrenheitFromCelsius m_Celsius
   'Set the Background color to
   'match the container's background
   BackColor = Ambient.BackColor
   'Set the caption to be the
   'same as the object's initial name
   'm_Caption = Extender.Name
   'and set Slider to reflect temperature
   Slider1.Value = m_Celsius
   'and textbox to match date
   txtDate.Text = m_TemperatureDate
End Sub

Note in the listing that variable names storing property values do not follow the usual Hungarian notation naming convention (a two-four letter lowercase prefix indicating variable type). Instead, variable names that refer to the intermediate storage of the values of properties implemented with Property Get/Let/Set are prefixed with "m_"—a Microsoft documentation standard for Class modules. Microsoft's Control Wizard generates such variable names, automatically deriving them from the names of properties that you specify in the wizard.


<< Previous | Contents | Next >>


Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved