Home   |  Viewing problem solution  |  About me


 

Active X

 
Creating Active X controls in languages other than Visual Basic might seem terrific to naive programming gurus. Microsoft Visual Basic has made the job of creation of COM components (formerly known as OLE components) and Active X components a lot easier.

Here I am going to demonstrate that how you can use Microsoft Visual Basic 6 (sorry, I don't have a fully functional Visual Studio .Net with me) to create a simple Active X component.

Before getting into the codes, let us first be familiar with Active X technologies. COM as you know is a TLA (three letter acronym) for Component Object Model. Software developers all around the world are facing the problem of incompatibilities between software from different organizations and vendors. OLE (or object linking and embedding) made it possible to let an application expose its functionality and objects to other applications. However, Microsoft went a step further and created Active X specifications. Active X is not a new language - it is rather a set of rules (a blueprint) for a component that makes it possible for the component to expose its functionality (data, object, methods and interfaces) to the other applications. In short it provides the building block for the component interoperability. A component is a piece of precompiled software that can be plugged into any application with either no or minimal re-coding. Active X components are different from conventional components in the fact that they can be used from within the Web-enabled applications, such as a Web browser (an example, Microsoft Internet Explorer). This allows the programmer to show off his products to the entire world, while sticking at the languages and tools he is comfortable with.

Okay, enough is enough. Let's begin with the sample I built using Active X standard in Visual Basic.

Specs: I am going to create a spin box control (component). A spin box control allows the user to increment/decrement the value of a counter manually/automatically through the use of appropriate buttons.

What You Need: Microsoft Visual Basic Professional Edition, Windows platform (9x/Me/NT/XP).

Prerequisite: Familiar with Visual Basic IDE and simple programming constructs.

Where to Start? Activate Microsoft Visual Basic by Start > Programs > Microsoft Visual Studio program group.

Concentrate: From the New Project dialog box, select New tab and then ActiveX Control list item. Click the Open button.
  1. Draw two Command Buttons and a Text Box control on the form.
  2. Set the Text Box properties as,

    Name: txtSpinBoxValue
    BackColor: &H000000C0&
    ForeColor: &H00FFFFFF&
    Height: 285
    Left: 240
    Locked: True
    MaxLength: 3
    Text: {blank}
    Top: 0
    Width: 430
     
  3. Set the Command Buttons properties as (single value for common settings, multiple values separated by 'and' means former for '+' and latter for '-'),

    Name: cmdPlus and cmdMinus
    BackColor: &H00E0E0E0&
    Caption: + and -
    Height: 285
    Left: 0 and 680
    Style: 1-Graphical
    Top: 0
    Width: 255
     
  4. Set the User Control properties as,

    Name: SpinControl
    Height: 285
    Width: 930
     
  5. Set the Project properties as,

    Name: KSpinControl
Now you need to add the following code in the Code Editor window,



Here (after studying the code) you will find that there are three exposed (public) properties namely Min, Max and Interval. Min and Max, as the name specify binds the control to range its value from Min to Max. User, by clicking the '+' button can increment the contents of the spin box control (but this value can't exceed Max). Similarly the user, can click '-' button and decrement the contents of spin box, however the value can't go beyond the Min specified.

Now another question pops up - By how much amount the contents of spin box be inc(dec)remented? The answer to this question is by the amount specified by Interval property. The user can specify a separate valid value for each of these properties and can use the control in his application.

Min, Max and Interval properties are public in nature, but internally (within the component), I am storing the value of these properties in private member variable namely m_Min, m_Max and m_Interval respectively. Part of this code has been generated by the "VB6 ActiveX Ctrl Interface Wizard", if you also want to use this option you will need to enable it from Add Ins > Add In Manager menu option. Once done, run the wizard and add appropriate public properties to the control.

You can set the attributes (such as description) to the property procedures of your control. This will help the user to understand what a control's specific property do. To do this run Tools > Procedure Attributes. Select the appropriate property procedure and then add the description (that specifies the significance of that property).

Make Component: Now, the whole job is done. Select File > Make KSpinControl.ocx... menu item. This will create the desired .ocx file (the ActiveX control) for you. In order to use an ActiveX control on your system, you need to first register it with the registry database of your system. When you select the menu item, just mentioned the Visual Basic, in addition to creation of .ocx file registers it for you automatically (registration is required, because then only your control will appear in the Components dialog box). However, if you manually want to register the control you can run the following command from the command prompt,

C:\> C:\Windows\Regsvr32 <your .ocx filename with complete path>

On the other hand to unregister the control use /u switch. Now you (and your clients) can use this ActiveX control just as they use any other control.

Testing: Create a new Standard EXE project. Go to Project > Components... Select KSpinControl from the list box and then click on OK button. A new control option will appear on toolbox. Click and draw it on the form. Now set its various properties and test the application by running it.

If you want to enhance the functionality of the control you can add additional property procedures and variables. If you do so kindly let me know. Till the next update keep coding and have fun.

You can download the sample source code (mentioned above) and OCX control from here (28 KB).

All rights reserved. Copyright © 1999 - . Krishna Kumar Khatri.
Best viewed with Microsoft IE 6 or later, under 800x600 resolution with True Color (24-bit).