|
|
Title | Create an ActiveX control with a parameterized property |
Description | This example shows how to create an ActiveX control with a parameterized property in Visual Basic 6. |
Keywords | ActiveX, property, parameter |
Categories | ActiveX, ActiveX Controls, Controls |
|
|
The Property Get and Property Let procedures take an extra parameter.
|
|
Public Property Get Caption(ByVal Index As Integer) As _
String
' Do not allow indexes < 1.
If Index < 1 Then
Err.Raise 380, "LabelList.Caption", _
"Caption index must be greater than zero."
End If
' If the index is beyond the end of the
' array, return the default value.
If Index > m_NumCaptions Then
Caption = m_def_Caption
Else
Caption = m_Caption(Index)
End If
End Property
Public Property Let Caption(ByVal Index As Integer, ByVal _
New_Caption As String)
' Do not allow indexes < 1.
If Index < 1 Then
Err.Raise 380, "LabelList.Caption", _
"Caption index must be greater than zero."
End If
' Make room if necessary.
If Index > m_NumCaptions Then
m_NumCaptions = Index
ReDim Preserve m_Caption(1 To m_NumCaptions)
End If
m_Caption(Index) = New_Caption
PropertyChanged "Caption"
Refresh
End Property
|
|
The program's InitProperties, ReadProperties, and WriteProperties event handlers are modified to handle the parameterized property values. In this example, that means looping over the property values to save or restore them.
|
|
'Default Property Values:
Const m_def_Caption = """"
'Property Variables:
Dim m_Caption() As String
Dim m_NumCaptions As Integer
'Initialize Properties for User Control
Private Sub UserControl_InitProperties()
m_NumCaptions = 0
End Sub
'Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As _
PropertyBag)
Dim i As Integer
' Read the number of captions.
m_NumCaptions = PropBag.ReadProperty("NumCaptions", 0)
If m_NumCaptions > 0 Then
' Allocate room for the captions.
ReDim m_Caption(1 To m_NumCaptions)
For i = 1 To m_NumCaptions
m_Caption(i) = PropBag.ReadProperty("Caption(" _
& Format$(i) & ")", m_def_Caption)
Next i
End If
End Sub
'Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As _
PropertyBag)
Dim i As Integer
Call PropBag.WriteProperty("NumCaptions", _
m_NumCaptions, 0)
If m_NumCaptions > 0 Then
For i = 1 To m_NumCaptions
Call PropBag.WriteProperty("Caption(" & _
Format$(i) & ")", m_Caption(i), _
m_def_Caption)
Next i
End If
End Sub
|
|
|
|
|
|