2005-09-25
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 3 of 5 )
Custom Properties
When you subclass a control, you can make the custom control property either a totally new property or an override of an inherited property. In your custom control's code, you use the MyBase keyword to refer to the base class. This lets you override a property while still accessing the base class's property of the same name.
To create a new property, you follow the usual procedures for defining a class property. This involves writing a property procedure for the property. In some cases, you also declare a private class variable to hold the property value. The second step is not required if the property value is stored elsewhere (such as in a base class property) or is calculated on the fly.
Let's look at two examples. This first example defines a new property called Flavor, of data type String.
Private pFlavor As String
Public Property Flavor() As String
Get
Flavor = pFlavor
End Get
Set(ByVal Value As String)
pFlavor = Value
End Set
End Property
This second example is intended to be used in a custom control that subclasses a TextBox control, or any other control that has aText property but no Length property. It defines a read-only property named Length that returns the length (number of characters)
in the Text property.
Public ReadOnly Property Length() As Long
Get
Length = Len(MyBase.Text)
End Get
End Property
The third example overrides a property that exists in the base class, using the Overrides keyword in the property procedure definition. The new property can completely replace the property it overrides, or it can interact with the base class property (again, using the MyBase keyword).
Here is an example of overriding the Text property in a control that subclasses the TextBox control. The result is that the TextBox always displays text in uppercase, while the text that is stored remains the original combination of upper- and lowercase. This works only when the Text property is assigned in code, and not when the user enters text directly into the control (the latter could be programmed as well, if desired, but is not done here).
Private pText As String
Public Overrides Property Text() As
String
Get
Text = pText
End Get
Set(ByVal Value As String)
pText = Value
MyBase.Text = UCase(Value)
End Set
End Property
![]() |
|


