ActiveX Components

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

An ActiveX Component may either be an ActiveX EXE, ActiveX DLL or an ActiveX Control.

ActiveX Controls

An ActiveX control is a component that may be added to the Form, like the controls in the ToolBox. You can build three different types of ActiveX control in Visual Basic.

You can build an ActiveX Control without using any existing controls, designing your control completely from scratch.

You can build a control that takes an existing control and extends its functionality.
For example validating data entered into a TextBox.

You can build an ActiveX control that is made up of existing controls (Constituent Controls).
For example grouping a Label and a TextBox to make a control that provides text input with a read-only prompt.

To create an ActiveX control, start a new project and select ActiveX Control as the project type. We will develop a control called RGBMixer, so change the Name property to RGBMixer. The first stage is to draw the Constituent Controls.

DEVELOPMENT OF AN RGBMixer CONTROL

Add three HSCrollBars in a Control Array called hsbColour. For the scrollbars, set the Min property to 0, the Max property to 255, the SmallChange property to 1 and the LargeChange property to 16. Add three Labels in a control array called lblPrompt, and set the Caption property to R, G and B. Add three further labels in a control array called lblColour and set their Caption property to 0. Finally, add a Label called lblInkPot, remove the caption and set the BackColor property to black. Layout the controls as below.

When a scrollbar is clicked, the colour of the lblInkPot control changes according to the values of the three scrollbars used to represent the intensity of Red, Green and Blue. The number beside the scrollbar indicates the value.

BASIC FUNCTIONALITY OF THE RGBMixer:

Option Explicit
' Define the behaviour of the control
Private Sub hsbColour_Change(Index As Integer)
     lblColour(Index).Caption = hsbColour(Index).Value
     setColour hsbColour(0).Value, hsbColour(1).Value, hsbColour(2).Value
End Sub
Private Sub setColour(ByVal r As Single, ByVal g As Single, ByVal b As Single)
     lblInkPot.BackColor = RGB(r, g, b)
End Sub
Private Sub hsbColour_Scroll(Index As Integer)
     lblColour(Index).Caption = hsbColour(Index).Value
     setColour hsbColour(0).Value, hsbColour(1).Value, hsbColour(2).Value
End Sub

 

PPOPERTY BAG OBJECT

The PropertyBag is an object that allows the Property data to be written to a file in order it remains persistent. This means that when users of the control change the Properties at design-time, the changes remain the next time they use the project.
For example, if you change the Caption property of a Label at design-time, you expect the value to be retained the next time you open the project.

The PropertyBag's ReadProperty method is used to read properties from the file and the WriteProperty method is used to write properties to the file. In the case of using the control in a Form, the data is written into the Form where the Properties are defined at the top.

The following are the Properties for a form containing the RGBMixer control.

Begin RGBMixerExample.RGBMixer rgbMix
     Height = 930
     Left = 1200
     TabIndex = 0
     Top = 3120
     Width = 4110
     _ExtentX = 7250
     _ExtentY = 1640
     Red = 51
     Green = 153
     Blue = 204
End

 

PROPERTY BAG READPROPERTY

The ReadProperty method is used to read Property values for the ActiveX control from a file. The ReadProperty method takes two parameters, the name of the Property to be read and a default value. The WriteProperty method uses the default value to determine whether or not to write the property to file. If the Property value is equal to the default value, it doesn't write the value to file to save space.

The following is the ReadProperties Event for the RGBMixer control.

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
     hsbColour(0).Value = PropBag.ReadProperty("Red", 0)
     hsbColour(1).Value = PropBag.ReadProperty("Green", 0)
     hsbColour(2).Value = PropBag.ReadProperty("Blue", 0)
End Sub

 

PROPERTY BAG WRITEPROPERTY

The WriteProperty method is used to write Property values for the ActiveX control to a file. The WriteProperties event fires in response to the PropertyChanged method. The WriteProperty method takes three parameters, the name of the Property to be written, the value for the property, and an optional default value. If the value to be written is the same as the default value, nothing is written to the file to save space.

The following is the WriteProperties Event for the RGBMixer control.

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
     PropBag.WriteProperty "Red", hsbColour(0).Value, 0
     PropBag.WriteProperty "Green", hsbColour(1).Value, 0
     PropBag.WriteProperty "Blue", hsbColour(2).Value, 0
End Sub

 

PROPERTIES FOR RGBMixer

Properties for the control can be added using the Property statement. The Properties we shall expose in RGBMixer are Red, Green, Blue and Colour.

Public Property Get Red() As Single
     Red = hsbColour(0).Value
End Property
Public Property Let Red(ByVal r As Single)
     hsbColour(0).Value = r Mod 256
     UserControl.PropertyChanged "Red"
End Property
Public Property Get Green() As Single
     Green = hsbColour(1).Value
End Property
Public Property Let Green(ByVal g As Single)
     hsbColour(1).Value = g Mod 256
     UserControl.PropertyChanged "Green"
End Property
Public Property Get Blue() As Single
     Blue = hsbColour(2).Value
End Property
Public Property Let Blue(ByVal b As Single)
     hsbColour(2).Value = b Mod 256
     UserControl.PropertyChanged "Blue"
End Property
Public Property Get Colour() As OLE_COLOR
     Colour = RGB(hsbColour(0).Value, hsbColour(1).Value, hsbColour(2).Value)
End Property
Public Property Let Colour(ByVal c As OLE_COLOR)
     hsbColour(0).Value = c And &HFF
     hsbColour(1).Value = (c \ &H100) And &HFF
     hsbColour(2).Value = c \ &H10000
End Property

 

EVENTS FOR RGBMixer

You can declare whatever events you want with a control. For the RGBMixer, we shall expose a Change event to allow the user of the control to handle Change events. The Change event is exposed with the line:

Public Event Change()

 

Having exposed the event, we now use the RaiseEvent function to determine when the Change event will fire.

Private Sub hsbColour_Change(Index As Integer)
     lblColour(Index).Caption = hsbColour(Index).Value
     setColour hsbColour(0).Value, hsbColour(1).Value, hsbColour(2).Value
     RaiseEvent Change
End Sub

 

THE AMBIENT PROPERTY

The Ambient Property contains the ambient properties of the container. We can use this to change the BackColor Property of our control so that it blends in with the container.
For example the Form it is added to.

Private Sub UserControl_AmbientChanged(PropertyName As String)
     Dim counter As Single
     If PropertyName = "BackColor" Then
          UserControl.BackColor = UserControl.Ambient.BackColor
          For counter = 0 To 2
               lblPrompt(counter).BackColor = UserControl.Ambient.BackColor
               lblColour(counter).BackColor = UserControl.Ambient.BackColor
          Next counter
     End If
End Sub
Private Sub UserControl_Show()
     Dim counter As Single
     UserControl.BackColor = UserControl.Ambient.BackColor
     For counter = 0 To 2
          lblPrompt(counter).BackColor = UserControl.Ambient.BackColor
          lblColour(counter).BackColor = UserControl.Ambient.BackColor
     Next counter
End Sub

 

The UserControl's ToolboxBitmap may be used to associate a graphic with the control. The size of the bitmap should be 16 by 15 pixels. When the control is added to the ToolBox, the ToolboxBitmap is what the user will see.

You can add Property Pages to the control to provide the user of the control a convenient means of the amending properties for the control. Once the control has been written, start a new project and add the RGBMixer. We can now develop programs that rely on mixing colours easily as most of the work is already done.

THE EXTENDER PROPERTY:

The Extender Property is an instance of the Extender Object that has the properties of the control that are controlled by the container of the control rather than by the control itself. The Extender Object allows you to read and set properties of the container.
For example, to set the background to the background colour of the container object.

UserControl.BackColor = UserControl.Extender.Container.BackColor

 

Some properties only exist in certain objects. The control has no way of knowing what Properties are available when it is run so you should write code to ensure the properties set exist. One of doing this is to use the TypeOf method.
The following example checks to see if the container is a Frame.

If TypeOf UserControl.Extender.Container Is Frame Then
     ' Set or read properties for a Frame
End If

 

Properties should be set in the Container object with care, as you're removing the Developer's ability to set up the control how they want it. The following example sets the Caption property of the Container to today's Date.

UserControl.Extender.Container.Caption = Format(Date, "Long Date")

 

 

ActiveX DLLs

An ActiveX DLL is intended to be used by another component. An ActiveX DLL is a Class whereby you can define attributes and methods that may be used in another project. An ActiveX DLL is an in-process component in that it runs in another application's process.

To illustrate ActiveX DLLs we shall build a Class that uses Microsoft Word's spell checker (Word must be installed on the computer the program is run on).

To create an ActiveX control, start a new project and select "ActiveX DLL" as the project type. In the "References" menu item, select "Microsoft Word Object Library". The first stage is to define the ActiveX DLL Class.

Defining the ActiveX DLL Class

The Class name used in this example is "CSpellChecker". When the example is compiled to a DLL, it is registered in Windows and may be viewed through the "Project", "References" menu item. The name used for the DLL will be the Project Description if one was provided, otherwise the name of the project. Set the project description for this example to "Spell Check Text". Once the class is defined as below, compile the DLL from the "File" menu.

Option Explicit
Private mMSWord As Word.Application
Private Sub Class_Initialize()
     Set mMSWord = New Word.Application
End Sub
Public Function checkWord(ByVal s As String) As Boolean
     checkWord = mMSWord.checkSpelling(s)
End Function
Private Sub Class_Terminate()
     mMSWord.Quit
     Set mMSWord = Nothing
End Sub

 

Having defined the class, we can now build a Standard EXE project to act as a client for the our ActiveX DLL. In the new project, select "Spell Check Text" from the "References" menu item. This allows us to use the functionality of the DLL we created in the first stage.

Form to use the CSpellChecker DLL

The form uses two controls -
1. A TextBox named txtWord,
2. A Label named lblStatus.

Option Explicit
Dim sc As CSpellChecker
Private Sub Form_Load()
     Set SC = New CSpellChecker
End Sub
Private Sub Form_Terminate()
     Set SC = Nothing
End Sub
Private Sub txtWord_GotFocus()
     txtWord.SelStart = 0
     txtWord.SelLength = Len(txtWord.Text)
End Sub
Private Sub txtWord_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          If sc.checkWord(txtWord.Text) = True Then
               lblStatus.Caption = "Correct"
          Else
               lblStatus.Caption = "Incorrect"
          End If
     End If
End Sub

 

 

ActiveX EXEs

The purpose of all of the ActiveX components is to provide reusable code in the form of objects. An ActiveX EXE is similar to the ActiveX DLL, except that it is an out-of-process component, in that it runs in its own address space. The client is usually an application running in another process. An ActiveX EXE may run as a stand-alone application or through automation.

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641