ActiveX Controls in Visual Basic
Ability to create ActiveX
Controls in Visual Basic was introduced in its fifth version. Version 6.0 of Visual Basic enhanced this
feature by adding some more functionality and better performance.
In addition to common UI
components Visual Basic provides us with many other ActiveX controls like ADO
Data Control, Status Bar, ToolBar, Data Combo and DataList controls. We can also create our own ActiveX controls.
There are three methods for
creating controls in Visual Basic:-
1)
Create Our
Own Controls from Scratch
We don’t rely
on existing controls but create our own control with Visual elements and
behavior of our choice. This type of
control is called a user-drawn control.
2)
Enhance a
Single Existing Control
Enhancing an
existing control implies adding our own properties, methods and events to an
existing control. We have complete
freedom in specifying the interface for our enhanced control.
3)
Assemble a
new Control from Several Existing Controls
Visual Basic
provides us with user control. It is
similar to a form without the borders.
We place existing controls on a user control object and aggregate them
into a single ActiveX control. The
existing controls are called constituent controls.
Control Class
The control that we develop in
Visual Basic is actually a control class, a description from which controls can
created. When we put a control on a
form, we are actually creating an instance of this control class.
An ActiveX control that is
created by placing multiple UI controls in a User-Control. The user control forms the base on which
ActiveX is built.
We can set the properties of
individual controls; add code etc, to create an ActiveX that we want. This ActiveX created by the user is stored in
a plain text file that contains the code and properties of user control and
constituent controls. These files are
stored with a ‘.CTL’ extenstion.
If a user control or its constituent
controls use graphical elements like bitmaps, which can not be stored as a text
the Visual Basic stores them in a ‘.ctx’ file.
When a ActiveX control project is
compiled, control with a ‘.OCX’, extension.
Each ‘.ctl’ file defines separate control class. A ‘.OCX’ file contains all controls of the
project.
Creating An ActiveX Control
Simplest method of creating
ActiveX control is to use controls that already exist. In Visual Basic, an ActiveX control consist
of a user control object with constituent controls placed on user control.
We add constituent controls to
user control object in the same way that we add controls to a Visual Basic
form.
In Visual Basic, to create an
ActiveX control first step is creating a project file of the type ‘ActiveX Control’. This project will not have a form but a user
control. This is similar to a form
without the borders. Existing or
constituent controls can be placed on the user control form just as we place
them on standard form. We can also set
their design time properties and write code for the events. Number pf properties are associated with user
control object. Most of those are
similar to the properties of the form.
Its important properties are:-
1)
BackStyle
Property
Returns or sets
a value indicating whether the background of constituent control is transparent
or opaque. This property is set to 0 for
transparent. It means that when user
control is placed in a form, all user sees are the controls like text boxes,
list boxes etc. and not the user control on which they are placed. When property is set to 1 for opaque, the
user will see user control plus other controls.
2)
ToolBoxBitmap
Property
Sets a bitmap
that will be used to represent the control in the toolbox. By default, size of the bitmap in toolbox is
16 X 15 pixels. If specified bitmap is
larger than 16 X 15 it will be scaled to fit this size. This property is not available at run time. It is important to note that once the
multiple controls are combined to create an ActiveX control, the properties,
methods and events of individual constituent controls are unavailable to user
of ActiveX control. To be able to access
them, we need to add custom properties, methods, and events to them and
associate these custom characteristics with individual controls
characteristics.
Custom Properties
Custom properties can be written
either by using property procedures or by using ActiveX control wizard. Property procedures consist of property Get,
property Let and property Set procedures.
Using ActiveX Control Interface Wizard
Visual Basic provides with an
ActiveX control interface wizard that can be used to add properties, methods
and events to the control.
A wizard can be invoked or added
to the list of ‘Add-Ins’ using Add-In-Manager.
To start wizard we have to select it from ‘Add-In’ Menu. By following necessary step the wizard can be
utilized.
The wizard creates codes for the
properties, methods and events that we have created using details specified at
the various stages.
Property Pages
The ActiveX controls that are
created in Visual Basic can be used with other procedures like Visual C++,
Power Builder, and Developer 2000 etc.
Visual Basic provides with a ‘Properties’ window at design where all
properties can be seen.
The Client application can always
read type library to determine available properties and their types but it
would be better if ActiveX was to provide a Visual representation of its
properties. This is done via property
pages.
The status bar control is the one
control that provides property pages. On
clicking custom in the properties window for status bar control we can see
property page. With the help of property
pages we get to view and change related properties of a control at the same
time.
Creating Property Pages
Visual Basic provides some
pre-created property pages for selecting options like font and color. These can be associated with properties that
affect font and color of ActiveX. In
addition, it is possible to create property pages that handle custom
properties.
Visual Basic provides property
page wizard which is used to add a property page for our control. The ‘VB6 Property Page Wizard’ is also an
Add-Ins and can be loaded via Add-In-Manager.
By following necessary steps
Property Pages can be created.
Property Values
For every property, wizard
generates a property Let and property Get method. Property Let gets called whenever user
changes the value of the property.
Property Get is called whenever user tries to retrieve value of
property.
Example
à Public Property Get User_Password ( ) As String
User_Password =
TxtPassword(1).Text
End Property.
In the above code TxtPassword(1)
is TextBox control name to enter password.
To retrieve value Set by the user, above method is used. As also, each time a property is to be
changed the following code gets executed:-
à Public Property Let User_Password (By Val
New_User_Password As String)
TxtPassword(1).Text
( ) = New_User_Password
Property Changed
“User_Password”
End Property.
First line of the code just
displays new value of the property in the ActiveX. Client application must be notified of the
change in value of ActiveX control’s property.
This is the role of method ‘Property Changed’.
Syntax:
PropertyChanged Property Name
PropertyBag Object
A propertybag object holds
information about a control’s properties as the control is destroyed and
recreated. For example:- the properties
of command button like its caption, backcolor are saved in command button’s
propertybag object.
Custom properties are read and
written to propertybag using ReadProperties and WriteProperties events. These events use PropertyBag as a parameter.
Saving Property Values
PropertyChanged method only
notifies that the value of property has changed. If the value has to be saved in PropertyBag
code is written using write properties event.
We use this event to store each property in the property bag object
Syntax: (Write Properties)
àSub
Obect_WriteProperties(Propbag As PropertyBag)
Where :
Object
Is the name of
user Control.
Propbag Is an object of the type PropertyBag
class to which we write data.
From WriteProperties event WriteProperty
method of PropertyBag object is used.
This method specifies name of the property, the current value and
default value of the property.
Syntax (Write Property)
à Object.WriteProperty (Name, Value,
DefaultValue)
Object Is the name
of user Control
Following code shows how a custom
property can be written to a PropertyBag using writeProperties event.
à Private sub UserControl_WriteProperties (Propbag As
PropertyBag)
PropBag.WriteProperty
(“BorderStyle”, UserControl.BorderStyle,0)
End
Sub
Retrieving Property Values
If property values are saved they
can e retrieved. ReadProperties event
for user control is triggered whenever the project containing our control is
loaded. This enables to retrieve
property information from the PropertyBag object.
Syntax: (Read Properties)
à Sub Object.ReadProperties (PropBag As PropertyBag)
From ReadProperties event we use ReadProperty
method and default value. We specify
default value in case the value had not been saved.
Syntax: (Read Property)
à Object.ReadProperty (Name, [Default Value])
à Private Sub UserControl_ReadProperties (PropBag As
PropertyBag)
UserControl.BorderStyle
= PropBag.ReadProperty (“BorderStyle “,0)
End
Sub
Above code retrieves Border Style
Property from PropertyBag object using Read Property Event.
Top
No comments:
Post a Comment