Creating ActiveX Controls

FDN » Visual Basic » Creating ActiveX Controls

Creating ActiveX Controls

ActiveX controls are reusable UI components packaged as .ocx files. They can be used in VB6 forms, Internet Explorer web pages, Office documents, and any ActiveX container.

Creating a Custom Progress Bar

  1. Start a new ActiveX Control project in VB6.
  2. The project creates a UserControl. This is the design surface for your control.
  3. Add a PictureBox named picBar (the filled portion) and set its BackColor to vbBlue.
' UserControl code: FlameProgressBar
Option Explicit

Private m_lngValue As Long
Private m_lngMax As Long

Public Property Get Value() As Long
    Value = m_lngValue
End Property

Public Property Let Value(ByVal lngNew As Long)
    If lngNew < 0 Then lngNew = 0
    If lngNew > m_lngMax Then lngNew = m_lngMax
    m_lngValue = lngNew
    RedrawBar
    PropertyChanged "Value"
End Property

Public Property Get Max() As Long
    Max = m_lngMax
End Property

Public Property Let Max(ByVal lngNew As Long)
    If lngNew < 1 Then lngNew = 1
    m_lngMax = lngNew
    RedrawBar
    PropertyChanged "Max"
End Property

Private Sub UserControl_Initialize()
    m_lngMax = 100
    m_lngValue = 0
End Sub

Private Sub UserControl_Resize()
    RedrawBar
End Sub

Private Sub RedrawBar()
    If m_lngMax = 0 Then Exit Sub
    Dim pct As Double
    pct = m_lngValue / m_lngMax
    picBar.Move 0, 0, UserControl.ScaleWidth * pct, UserControl.ScaleHeight
End Sub

' Persistence
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    m_lngMax = PropBag.ReadProperty("Max", 100)
    m_lngValue = PropBag.ReadProperty("Value", 0)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "Max", m_lngMax, 100
    PropBag.WriteProperty "Value", m_lngValue, 0
End Sub

Compiling and Using

  1. Go to FileMake .ocx. This compiles and registers the control.
  2. In a Standard EXE project, go to ProjectComponents and check your control.
  3. It appears in the Toolbox. Drag it onto a form and set Value and Max properties.

Internet Explorer Usage

ActiveX controls can be embedded in web pages using the <OBJECT> tag. The control must be signed with Authenticode and marked safe for scripting.

« Developer Network ‹ VB6 Database Programming VB6 Winsock Networking ›