Create Your Own ActiveX Control With Visual Basic 6

This time I try to share one of the examples of activex control project is quite simple that is making a timepiece, well without any longer let's start



Open the new project with ActiveX Control project type, different from the usual project that is displayed is not the form but the user controls, change the name of the project into MyWatch, then change its user controls to Watch, BorderStyle becomes 0 - None


Then insert a label into the form and set its property to:
Name: lblJam
Appearance: 0 - Flat
Autosize: True
BackColor: & H00E0E0E0 &
BorderStyle: 1 - Fixed Single
Caption: 00:00:00
Font: Ms Sans Serif
Font Style: Bold

The next step we enter a Timer, then set its properties
Name : tmrJam
Interval : 1000

Then move the label that we made to the top left corner of the form, then minimize the size of the form of the label we created earlier


Well until here we save the first project that we created, for the user controls give the name MyWatch.ctl file, for his project give the name MyWatch.vbp

After the design interface is complete, when it is to add code into the project, double click on user controls add the following code:

Private Sub UserControl_Initialize()
    lblJam.Caption = Time
    tmrJam.Enabled = True
End Sub

The Initialize sub procedure will be loaded first when the component is first loaded on the form, then add the following code:

Private Sub tmrJam_Timer()
Dim jam
    jam = Time()
    lblJam.Caption = jam
End Sub 

The above procedure will be called when the component is dragged or slid on the form, to specify the font to use, we use the SET function and to read the value we use the GET function, add the following code:

Public Property Set Font(ByVal NewFont As Font)
    Set lblJam.Font = NewFont
    PropertyChanged "Font"
End Property

Public Property Get Font() As Font
    Set Font = lblJam.Font
End Property 

we know each activex control that we have drag into a form then each control will have property value, with SET procedure we can specify some value that we will change in its properties, while the GET procedure later that will take value on the properties and will be applied when an activex control is dragged or slid into a form

Then we add an Event that will be triggered in the event of a change in the activex control

Make sure your cursor is in position (General) (Declaration),

Then type the following code:

Public Event change() 

This event we need to detect if there is a change in the activex control that will run a procedure we named SetTime (), type the following code:

Private Sub SetTime(ByVal waktu As String)
    lblJam.Caption = waktu
    RaiseEvent change
End Sub 

This procedure will trigger Event change () to run, which will parse a time variable and adjust it to the clock label

To read changes to global properties for fonts, we read through ReadProperties on the UserControl object, in this section the global properties will be read

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    lblJam.Font = PropBag.ReadProperty("Font", Ambient.Font)
    lblJam.FontBold = PropBag.ReadProperty("FontBold", _
                      Ambient.Font.Bold)
    lblJam.FontSize = PropBag.ReadProperty("FontSize", _
                      Ambient.Font.Size)
End Sub 

To write the changes that occur on the global property for the font, we do through WriteProperties on the UserControl object

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("Font", lblJam.Font, Ambient.Font)
    Call PropBag.WriteProperty("FontBold", lblJam.FontBold, _
         Ambient.Font.Bold)
    Call PropBag.WriteProperty("FontSize", lblJam.FontSize, _
         Ambient.Font.Size)
End Sub 

Procedure is what will make some property values into one unity or become part of the activex itself

Now it is almost finished, now we can save the project, then try to run the project press F5, and see the results that will be opened on the browser that we use

The next step is to compile the project we have created, click the File menu and select Make MyWatch.ocx, then specify the location of the folder where the ocx we will put it, then click the Ok button, now activex control we have successfully created

Now it's time we add the new activex control we created into our application project, open one of your projects, or create a new Standard.exe app, right click on Toolbox, select Components ..., after show click browse button, then search ocx file that we have created "MyWatch.ocx", then click Open, now in the Toolbox there will be a new component that is MyWatch, try to add to the form, the initial view of the ocx is the current time.