Wednesday, 10 August 2011

Make Your First ActiveX Control 3

0 comments
 
Implementing events
We know what the user wants to pop up when he
clicks the button - the Text in TextVariable.
Now we need to pop up the message box when the user click the button.
We want 2 events: KeyPress and Click.
First we need to declare them.
enter the following code to your form:

Event Click()
Event KeyPress(KeyAscii As Integer)
How do we know that the Click event not getting parameters,
and the KeyPress event get the KeyAscii parameter?
Double Click on the command button.
2 new lines have been inserted to your code:
Private Sub
Command1_Click()
End Sub
as you see, the Click event gets no parameters.
Now go to the button KeyPress event,
via the right ComboBox under the title bar,
which now showing the current event - Click
After you choose KeyPress from the combobox,
2 new lines were inserted to your code:

Private Sub Command1_KeyPress(KeyAscii As Integer)
End Sub


As you can see, the button KeyPress event get the KeyAscii parameter.

Implementing The KeyPress EventWe don't want to change the KeyPress Event. We want that the code the user will insert to the
KeyPress Event  will be launched as usual,  without any changes.
So we will enter the following lines to our form:

Private Sub Command1_KeyPress(KeyAscii As Integer)
    RaiseEvent KeyPress(KeyAscii)
End Sub
Code Explanation: when the user press on the Command1 Button,
simply launch the Control (MyControl) KeyPress event.
The 'RaiseEvent' function launch an event.
It launch the event with the KeyAscii parameter that has been
received from the Command1 KeyPress event.

Implementing The Click EventWe want to pop up the message box when the
Click event occur, and then run the code that
the user entered in the MyControl1 - Click event.

Enter the following code to your form:
Private Sub Command1_Click()
    MsgBox (TextVariable)
    RaiseEvent Click
End Sub
When the user Click on Command1, pop up a message box
with the TextVariable string.
Then run the code that the user inserted to the control Click event.
What will happen if you omit the 'RaiseEvent Click' line?
When the user will click the button, the message box will pop up,
and the code that the user entered to the MyControl1 Click
event will not be apply.
So actually the user will not be able to program the click event.

By now, your code should look like this:
Dim TextVariable As String

Event
Click()
Event KeyPress(KeyAscii As Integer)
Private Sub Command1_Click()
    MsgBox (TextVariable)
    RaiseEvent Click
End Sub
Private Sub Command1_KeyPress(KeyAscii As Integer)
    RaiseEvent KeyPress(KeyAscii)
End Sub
Private Sub UserControl_Resize()
    Command1.Width = UserControl.Width
    Command1.Height = UserControl.Height
End Sub
Public Property Get Text() As String
   
Text = TextVariable
End Property
Public Property Let Text(ByVal New_Text As String)
    TextVariable = New_Text
    PropertyChanged "Text"
End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    TextVariable = PropBag.ReadProperty("Text", "There is no message")
End Sub

Private Sub
UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("Text", TextVariable, "There is no message) 
End Sub

Compiling and running the control
Now lets see how the control is working by now.
Save the project (File->Save Project).

lets compile the project to OCX Control.
From the menu, choose   File->Make MyFirstOCX.ocx   and press OK.

Now your OCX Control has been created, AND registered with your system.

Start a new project and enter your control to the project
(From the menu choose Project->Components, mark the MyFirstOCX checkBox and press OK)
 Now you see the new control at the ToolBox.




Insert it to your form, resize it to your preferred size, and insert "hello" to the control Text property.Run the program and click the button. A "hello" message box is popping.



Leave a Reply