March 7, 2021
Hot Topics:

Beginning ActiveX - Part 1

  • By John Percival
  • Send Email »
  • More Articles »

Before we get going on properties, we need a new control to play with. Load up VB and start a new ActiveX Control Project. Play around with it a bit. Add some controls, play with some of the properties. To test your new control, close the designer and add a new standard project. You should find a new icon in your toolbox corresponding to your control, which you can use to draw your control like you would any other.

The simplest kind of property is a simple public variable. For example, if you put the following in the declarations section of your usercontrol:

Public EyeOpen As Boolean

You can now set this property to your heart's content. However, it will not do much because the control cannot tell when you are setting this property, and VB will not remember it between one session and the next. To do this, you must use property procedures.

Property procedures are called when a property value is set or retrieved. Lets make one now: open up the code window for your usercontrol and click on Tools|Add Procedure... Type in the name as 'EyeOpen' and the set the type to 'Property'. When you click OK, as if by magic, VB creates the skeleton for the property procedures. Now it is up to you to put the muscles on this skeleton.

As you can see, there are two: the 'Get' and the 'Let' functions (in fact there is also a third type that we will meet later). This may sound complicated, but in fact it is really easy: the 'Get' function is called when the container (the posh name for the form on which the control is placed) tries to get the current value for the property; the 'Let' function is called when the container tries to let the property have a new value.

For a property procedure to work, we must have a temporary variable to store the property's value in. Add the following to the declarations section:

Private m_EyeOpen As Boolean

Note that the 'm_' prefix is the prefix usually used for internal variables of a usercontrol.

Now to put some code in the Property Let procedure... This procedure has one parameter: by default it is called vNewValue, defined as a variant. We want a boolean not a variant because the eye can only be open or shut in our model; so, change this parameter to 'New_EyeOpen As Boolean'. If we want this property to be read only, you could leave this procedure empty. As it is, we want to set the m_EyeOpen to the new value, so add this one line of code, leaving the procedure looking like this:

Public Property Let EyeOpen(New_EyeOpen As Boolean)
m_EyeOpen = New_EyeOpen
'now do stuff to open/close eye as needed
End Property

The Property Get procedure is even easier! The Get procedure is similar to a standard function: you return a value by setting the function to equal something:

Public Property Get EyeOpen() As Boolean
EyeOpen = m_EyeOpen
End Property

There is just one more thing about properties: how do you get VB to remember the property value between sessions?

Page 5 of 7

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date