Wednesday, 10 August 2011

Make Your First ActiveX Control 4

0 comments
 
Adding more properties to the control
Now we want that the control will have all the Command Button properties.
lets add the BackColor property. Enter the following code to your form:
Public Property Get BackColor() As OLE_COLOR
    BackColor = Command1.BackColor
End Property
Public Property Let BackColor(ByVal New_BackColor As OLE_COLOR)
    Command1.BackColor() = New_BackColor
    PropertyChanged "BackColor"
End Property
Enter the following line to the UserControl_ReadProperties function:
Command1.BackColor = PropBag.ReadProperty("BackColor", &H8000000F)
Enter the following line to the UserControl_WriteProperties function:
Call PropBag.WriteProperty("BackColor", Command1.BackColor, &H8000000F)

The OLE_COLOR is the type of the BackColor property variable,
the same as the Boolean is the type of the Enabled property variable,
and the Integer is the type of the Height property variable.


What we did now is almost the same as we did with the Text property.
The difference is that in the text property we used
a variable (TextVariable) to store the property information.
Here we not using a variable, we read and write the information
directly to the Command1.BackColor property.

The Command1.BackColor property is here our variable that store
the information. Why is that?
Because when the user set the Control BackColor property,
we actually want to set the Command1 BackColor property.
Suppose the user set the Control BackColor to Black.
In that case, We want to set the Command1 BackColor to Black.
So actually, the Control BackColor property is the
Command1 BackColor property.
So instead of reading and writing to variable,
we read and write directly to the Command1 BackColor property.
It's exactly the same thing with all of the other properties.

Adding the rest of the properties
Public Property Get Enabled() As Boolean
    Enabled = Command1.Enabled
End Property
Public Property Let Enabled(ByVal New_Enabled As Boolean)
    Command1.Enabled() = New_Enabled
    PropertyChanged "Enabled"
End Property
Public Property Get Font() As Font
    Set Font = Command1.Font
End Property

Public Property Set Font(ByVal New_Font As Font)
    Set Command1.Font = New_Font
    PropertyChanged "Font"
End Property

Public Property Get Picture() As Picture
     Set Picture = Command1.Picture
End Property

Public Property Set Picture(ByVal New_Picture As Picture)
Set Command1.Picture = New_Picture
    PropertyChanged "Picture"
End Property

Public Property Get DisabledPicture() As Picture
     Set DisabledPicture = Command1.DisabledPicture
End Property

Public Property Set DisabledPicture(ByVal New_DisabledPicture As Picture)
    Set Command1.DisabledPicture = New_DisabledPicture
    PropertyChanged "DisabledPicture"
End Property

Public Property Get MousePointer() As MousePointerConstants
    MousePointer = Command1.MousePointer
End Property

Public Property Let MousePointer(ByVal New_MousePointer As MousePointerConstants)
    Command1.MousePointer() = New_MousePointer
    PropertyChanged "MousePointer"
End Property

Public Property Get MouseIcon() As Picture
    Set MouseIcon = Command1.MouseIcon
End Property
Public Property Set MouseIcon(ByVal New_MouseIcon As Picture)
    Set Command1.MouseIcon() = New_MouseIcon
    PropertyChanged "MouseIcon"
End Property

Public Property Get Caption() As String
    Caption = Command1.Caption
End Property
Public Property Let Caption(ByVal New_Caption As String)
    Command1.Caption() = New_Caption
    PropertyChanged "Caption"
End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Command1.BackColor = PropBag.ReadProperty("BackColor", &H8000000F)
    Command1.Enabled = PropBag.ReadProperty("Enabled", True)
    Set Font = PropBag.ReadProperty("Font", Ambient.Font)
    Set Picture = PropBag.ReadProperty("Picture", "")
    Set DisabledPicture = PropBag.ReadProperty("DisabledPicture", "")
    Command1.MousePointer = PropBag.ReadProperty("MousePointer", 0)
    Set MouseIcon = PropBag.ReadProperty("MouseIcon", "")
    Command1.Caption = PropBag.ReadProperty("Caption", "Button")
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("BackColor", Command1.BackColor, &H8000000F)
    Call PropBag.WriteProperty("Enabled", Command1.Enabled, True)
    Call PropBag.WriteProperty("Font", Font, Ambient.Font)
    Call PropBag.WriteProperty("Picture", Picture, "")
    Call PropBag.WriteProperty("DisabledPicture", DisabledPicture, "")
    Call PropBag.WriteProperty("MousePointer", Command1.MousePointer, 0)
    Call PropBag.WriteProperty("MouseIcon", Command1.MouseIcon, "")
    Call PropBag.WriteProperty("Caption", Command1.Caption, "Button")
End Sub

The difference between SET and GETAs you see, in some of the properties, we use SET instead of GET.
When you want to change the Command Button Picture property,
you press on the Button with the 3 dots on him that
found in the "Picture" cell and then browse for your picture.
When we want to set property that uses the browse button, we use SET instead of GET.

Leave a Reply