March 3, 2021
Hot Topics:

Beginning ActiveX - Part 2

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

The simplest way to prevent the user from writing to a property is to not include any code in the let/set procedure. This provides a way for you to easily feed back information to the developer and end-user.

Although you may not find that particularly useful, you may want to create controls that are read-only at run-time. Run-time is when the control is finally being used within a project, as opposed to design-time, which is when the control is being used in the design environment.

This can be done using the Usercontrol's AmbientProperties object. This object provides a property that returns true when the project is in run-time mode, the UserMode property. You can check this property at the beginning of the let/set procedure and then raise an error if there is a problem:

Public Property Get MultiLine() As Boolean
MultiLine = m_MultiLine
End Property

Public Property Let MultiLine(ByVal New_MultiLine As Boolean)
If Ambient.UserMode Then 
Err.Raise 382
Exit Sub
m_MultiLine = New_MultiLine
PropertyChanged "MultiLine"
End Property

This code only allows the property to be set at design-time. If the program tries to set it at run-time, the error 'Property is read-only at run-time' is raised.

The AmbientProperties object provides many more properties relating to the container of the control. These include the ways in which the container suggests the control does things. Although most of these can be ignored, some, such as the UserMode property, may be useful.

Similar to the AmbientProperties object is the Extender object. The Extender object holds properties of the control that are actually controlled by the container of the control rather than by the control itself. VB provides a host of useful properties on top of the ones that it is necessary for the container to provide. Because of this, you must be careful to make sure that the container of your control will always be VB, or that it at least supports the properties that you are accessing.

Page 3 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