What steps need to be taken to create a COM Component?



Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here




From: Eric Teutsch
Date: Wednesday 27 February 2002 2:30 AM
Subject: Creating COM component in VB6 question
Question: Hi !
I hope you can help. I'm a C++ guy switching a little over into VB cause it's so much faster to develop. Some months back I've built a small ActiveX helper component that is in use with a number of programs at various clients' sites. They now want me to improve something which requires a change to the public interface of the ActiveX component. From my C++ days I know what that would entail in terms of the IDL file and the interface definitions. I would add a second interface that inherits from the first, so that the original interface is immutable. Version 1 programs would continue to use the original interface, Version 2 programs would use the new interface which has everything the old one had, plus. Now how do you do that in VB? And how do I set the Component properties in the project properties? I appreciate any info you can send me, or references to websites, or even just the title to the perfect book.

Eric T.

There are 5 things you need to do to add a property into an ActiveX component:

1. Declare a variable to allocate space for the property (at the very top of the code):

Dim NameOfPerson As String

2. Add the property procedures.

Choose Add Procedure from the Tools menu and add a Public Property called YourName.

Two procedures will appear - Get and Let.

Change Variant to the data type you need. For this example, we are using Strings, so change the word 'Variant' to 'String'.

3. Write the code.

In the Get procedure, write this code:

YourName = NameOfPerson

When the program requests the property, the control will return the value of NameOfPerson to the program.

In the Let procedure, write this code:

NameOfPerson = vNewValue
PropertyChanged YourName

4. Property Bag. So the program knows at runtime what properties have been set at design-time, you need to store the properties in something called the property bag.

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
NameOfPerson = PropBag.ReadProperty("YourName", "Martin")
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("YourName", NameOfPerson, "Martin")
End Sub

I have used "Martin" as the default value. The name of the property has to be held in quotes as a string.

5. You may also want to specify a default value for the property, use this code to do that:

Private Sub UserControl_Initialize()
NameOfPerson = "Martin"
End Sub

Martin Allen 1999 - 2011.  Last updated Tuesday 09 August 2011 08:08:57 PM +0100.