Wednesday, 10 August 2011

Make Your First ActiveX Control 2

0 comments
 



Starting the programming
Add 1 Command Button to your form (named Command1).
This is how your control will look like.
Your control is the form and everything on it.
We don't want that the control will be a form with a button on it,
we want that the control will be a button only,
without the form around him.
So resize the form to be at the size of the button exactly,
so you won't see the form.
So now the Control look like a button.
But it's not a button, it's a form with button on it.
But what if the user will resize the control at design time
(like you do to Command Button, after you enter it to your form)?

Suppose you have a regular form with a button on it,
and the form is resized to the button size (like in your current control).
When the user will resize the form, he will see the form
that was before 'under' the button.
The same thing will happen in our case.

To solve this problem, when the user resize the form (i.e the control),
we need to resize the button to fit the form and the form will be
again at the size of the button.
When the user resize the control, he actually try to resize the button.
Enter the following code to let the user resize the button
instead of resizing the form:

Private Sub UserControl_Resize()
   Command1.Width = UserControl.Width
   Command1.Height = UserControl.Height
End Sub
UserControl is the name of the Form/Control.
thus, UserControl.Width is the Form/Control width,
UserControl.Height is the Form/Control Height and UserControl_Resize()
is the event that occur when the user resize the control.

Implementing Control's PropertiesEvery control has few properties as default: Name, Left, Index, Tag, and more.
Our control will inherit those properties as default.
But we want that our control will have some properties that
he doesn't get as default, like Text - the text of the message box
that will pop when the user press the button.
Implementing Text PropertyWe have 2 occasions: when reading the Text property and
when changing the Text property.
The reading occasion occur when we want to
read the porperty that the user set.
For example if the user set the control Text property to "hello",
the reading result will return "hello".
Lets implement first the reading occasion.

Enter the following code to your form:
Dim TextVariable As String
The TextVariable will be the variable that holds for us the value of the Text property, therefore the String that will be inserted into the message box.

Enter the following code to your form:
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    TextVariable = PropBag.ReadProperty("Text", "There is no message")
End Sub


The function above says: read the control's "Text" property.
If the reading yield nothing, set as default the Text property
to be "There is no message".
We called to read the Text property,
now we have to implement the reading method:
Public Property Get Text() As String
   
Text = TextVariable
End Property
The TextVariable will hold the Text property value,
so we simply need to return the value of TextVariable.
TextVariable is a string, and the calling for reading the
Text property value will return string, therefore the 'As String' above.

Implementing The Writing MethodThe write occasion occur when the user change the Text property.
In that case, we need to update the variable that
holds for us the propery value (TextVariable).

Enter the Following code to your form:
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("Text", TextVariable, "There is no message")
End Sub

The function above says:
Write the new property value to TextVariable,
and update the Text Property.
If the writing to the TextVariable return nothing,
Set the Text property value to be "There is no message".

Now we have to implement the writing method,
where the new property value will be entered into the TextVariable.
Enter the following code to your form:
Public Property Let Text(ByVal New_Text As String)
    TextVariable = New_Text
    PropertyChanged "Text"
End Property

The new Text property value is passed with the New_Text parameter.
Of course this parameter have to be String, because the Text property holds String.
We set the TextVariable variable to hold the new Text property value.
Then we announce that the "Text" property has been changed.

Leave a Reply