sas

Topic: Creation of User Defined ActiveX Control


     So far we have been using the  ActiveX Controls which come along with the Professional Edition of VB-6.   We have said that the greatest power if VB-6 is its capacity to create user defined ActiveX controls which can be used for building complex applications.   We have also pointed out that one can visualize a scenario, when every complex application will be encapsuled in a single ActiveX control.   In this lesson we will see how one can go about building one’s own ActiveX control.

Building a Picture ActiveX Control:

     The Picture ActiveX Control is intended to print pictures in a Picture box  selecting bmp or ico or gif files by searching the directories.   

  1. Open the VB-IDE.
  1. Click ‘New Project’ from the file menu to open the New Project Dialog Box as shown in the figure 1.   Doubleclick on the ActiveX Control  icon.   You will get a user control form named as usercontrol1.   Open the property window of the form and change its name property as .rform1 and then change the name property of the project as rproj.   Drag a picture box, dir , drive and file controls from the Toolbox, size them and position them as shown in the figure 2.Open the code window and enter the codes as shown in the figure 2a.

Private Sub File1_DblClick()
Picture1.Picture = LoadPicture(File1.Path & "\" & File1.filename)
End Sub


Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

      Figure 2a

  1. Save the form and the project one by one with the same names you have given before.   The control form is stored as rform1.ctl and the project is stored as rproj.vbp.   Click ‘Make rproj.ocx’ from the file menu.    Save the ocx file as rproj.   Now you have created an ActiveX control by name rproj.rform   This ActiveX control cannot be used directly.   It has to be placed in a container before we make use of the ActiveX control.

Containers for placing ActiveX Controls:

     ActiveX controls can be placed  in any of the following four containers.

  1. VB Design Form
  2. ActiveX Control Pad
  3. Microsoft Excel 97
  4. Microsoft Word 97
  5. ActiveX Control Test Container

1. ActiveX control placed in VB Design Form:

      Open the Standard EXE Design Form.   At the bottom of the Toolbox, you must be able to find the icon by name rform for the new ActiveX control you have created..   If you don’t find it, open the component menu by clicking component from project menu.   You will find the ocx file by name rproj as shown in the figure 3.  Select it and click OK.   The icon by name rform will appear at the bottom of the toolbox.  Drag the new ActiveX control in the design form and size it so that all its components are clearly visible as shown in the figure 4.   Save the form as rrform and the project as rrproj.   Select start from the Run menu.   Now you can vary the drive, the directory and the files in the Run Mode..   Select a gif file and double click it.    You will get the corresponding image loaded in the picture box as shown in the figure 5.

a1

                                           Figure 1

a2

                               Figure 2

a3

                                     Figure 3

a4

                                     Figure 4

a5

                                   Figure 5

2. ActiveX Control  placed in ActiveX Control Pad:

     Open the Microsoft ActiveX Control Pad from the Program List.   It will present a blank HTML form .   Select ‘insert’ from the Edit menu.    You will get the list of ActiveX Controls in the alphabetical order as shown in the figure 6.   Click the control rproj.rform.   The control window and the property window will appear on the screen as shown in the figure 7.   Close the two windows.   You will find the code for the ActiveX control written between two object tags as shown in the figure 8.   Save the html file as rr.htm in the desktop directory.   Close the ActiveX control pad.   Click the icon rr which you see in the opening window.   Immediately Internet Explorer will be opened and give a warning message  ‘An ActiveX object on this page may be unsafe. Do you want to allow it to be initialize and be accessed by script’.   Press Yes.  The ActiveX control in dynamic mode will be displayed in it.   You can now play with the directories and files and display an image in the picture box as shown in the figure 9.

a6

                            Figure 6

a7

                           Figure 7

a8
Figure 8

a9
       Figure 9

3. Placing the ActiveX Control in the Microsoft Excel 97:

.     Open Microsoft Excel Worksheet.   From the View menu select Toolbars item.   Then select Control Toolbox option.   The control box will appear, floating over the spreadsheet.  At the bottom of the control box, you will see an icon with a crossed hammer and wrench.   If you click this icon the ActiveX component Menu will appear.   Select the item rproj.rform.   The menu will disappear and the cursor becomes a crosshair and will allow you to draw the ActiveX control on the Spreadsheet.   Exit from the design mode by clicking on the Design Mode Icon which appears in the beginning of the  control box.   The ActiveX control becomes dynamic.   You can select a gif file and display it in the picture box as shown in the figure 10

a10

                                   Figure 10

4. Placing the ActiveX control in the Microsoft Word 97 Document:

     Open Microsoft Word and open a blank document.   From the view menu select Toolbars and then  Control Toolbox.    You will get a menu of ActiveX controls.    Select the Control ‘rproj.rform’.   Exit Design mode by clicking on the design mode icon from the tool box.  The  ActiveX control form appears in the dynamic mode.   Now you can change the directories, drives and files.   Select a bmp file or gif file and display it as shown in the figure 11

a11

                  Figure 11

1.Microsoft ActiveX Control Test Container:

A new utility called the ActiveX Control  Test Container has been included in VB-6.   This tool allows you to load an ActiveX control that you have created in order to test.   From the start up menu select Microsoft Visual studio->Microsoft Visual Studio 6.0 Tools->ActiveX Control Test Container as shown in the figure 12.   You will be presented with a blank ActiveX Test Container window as shown in the figure 13.   Select Edit->Insert New Control.   You will get a list of ActiveX Controls as shown in the figure 14.   Select rproj.rform.   The ActiveX control becomes active and you can select a gif file for display as shown in the figure 15.    You can insert the Calendar Control and you will find that the calendar becomes dynamic.
  a12

                    Figure 12

a13

                              Figure 13

a14

                     Figure 14

a15

                                 Figure 15


Prev

 

 

 

 

 

 

 

 

 


footer back link


 

Error in my_thread_global_end(): 1 threads didn't exit