Microsoft & .NETVisual BasicActiveX Controls: The Definitive Guide

ActiveX Controls: The Definitive Guide content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Before VB5, you needed to buy third party controls
to do even simple tasks. VB 5 brought about a revolution in control creation, allowing
developers to build activex controls very quickly and easily.

In the first part of this article, we take you
through the basics of creating an ActiveX control in Visual Basic. We will explain the
basics behind controls and how you can use some cool techniques to create a decent

For this article you can use the control creation
edition of VB if you wish. This can be downloaded from the Microsoft web site.

Creating an ActiveX control is very simple. Open up
VB and choose ActiveX Control from the open dialog. The usercontrol looks very like a
form, and behaves in a similar way. Just try drawing some controls from the toolbox and
settings some properties. Easy.

A control’s greatest feature is the ability to have
properties. You have been using properties all the time. This line,
Text1.Text="" is using the Text property of the text box. When using properties
in VB you need to define procedures to set and return the value of the property. These are
called Let and Get property procedures.

Here are two typical property procedures:

Property Get Name() As String
End Property

Property Let Name(ByVal New_Name As String)
  PropertyChanged "Name"
End Property

Visual Basic provides you with an easy tool
to quickly generate these properties. This is called the ActiveX Control Wizard. You can
show this toolbar by selecting View, Toolbars, Add-In Toolbar. Click on this button and
choose the properties that you want. In this example, we want BackColor, Enabled, Font and ForeColor. We will deal with events later. On the next screen make sure that you map these properties to Usercontrol.

Now open up the code window for the control, and there you are,
lots of nice procedures generated by VB.

You will notice the ReadProperties and WriteProperties procedures.
These procedures actually control the storage of the properties. E.g. If you set the font
of a control at design time, run and then end the program you still want the control to
have the same font. These properties handle this.

They store the information in what’s called the controls
PropertyBag. This is just an area of memory dedicated to storing information about the
controls properties. You need to define calls to carry out these actions.

As you can see from the property procedures that the Let
procedures use the PropertyChanged keyword. This keyword alerts the control that a
property has been changed. Aside to that keyword, the property Let procedure can be used
to apply separate settings such as applying the new Font to the controls on your
ActiveX control.

For this example, rename the control ctlCaption. Draw a label on
to the usercontrol and rename it lblCaption. Set its AutoSize property to True. Now add
this code into the Usercontrol’s Resize method:

Private Sub UserControl_Resize()
'Resize the control based on the caption
lblCaption.Move ScaleLeft, _
                ScaleTop, _
                lblCaption.Width, _

If lblCaption.Width > 0 Then
  UserControl.Width = lblCaption.Width
  UserControl.Height = lblCaption.Height
End If
End Sub

The basis of this control is to act as a caption.
At the moment you can’t apply any of the Font, ForeColor or BackColor properties. We will
now add code to the Let procedures to make them change the label. Here are the new
property procedures:

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

Public Property Let ForeColor( _
         ByVal New_ForeColor As OLE_COLOR)
  lblCaption.ForeColor = New_ForeColor
  PropertyChanged "ForeColor"
End Property

Public Property Let BackColor( _
         ByVal New_BackColor As OLE_COLOR)
  lblCaption.BackColor = New_BackColor
  PropertyChanged "BackColor"
End Property

The property Get procedures also need to be ammended:

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

Public Property Get ForeColor() As OLE_COLOR
  ForeColor = lblCaption.BackColor
End Property

Public Property Get BackColor() As OLE_COLOR
  BackColor = lblCaption.BackColor
End Property

Add a Standard EXE project and draw our control
onto the form. Change the properties and watch the label change at the same time. But we
are missing one thing. The ability to change the caption. Paste these two procedures in:

Property Get Caption() As String
  Caption = lblCaption.Caption
End Property

Property Let Caption(ByVal New_Caption As String)
  lblCaption.Caption = New_Caption
  PropertyChanged "Caption"
End Property

We also need to change the WriteProperties and
ReadProperties procedures:

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
   lblCaption.BackColor = PropBag.ReadProperty("BackColor",&H8000000F)
   lblCaption.ForeColor = PropBag.ReadProperty("ForeColor",&H80000012)
   UserControl.Enabled = PropBag.ReadProperty("Enabled", True)
   Set lblCaption = PropBag.ReadProperty("Font", Ambient.Font)
   lblCaption.Caption = PropBag.ReadProperty("Caption","Caption Control")
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("BackColor", lblCaption.BackColor,&H8000000F)
    Call PropBag.WriteProperty("ForeColor", lblCaption.ForeColor,&H80000012)
    Call PropBag.WriteProperty("Enabled", UserControl.Enabled,True)
    Call PropBag.WriteProperty("Font", lblCaption.Font,Ambient.Font)
    Call PropBag.WriteProperty("Caption", lblCaption.Caption,"Caption Control")
End Sub

There you have your first activex control.

In the next part we will discuss events and how
you can make a better control.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories