Anda di halaman 1dari 2

A c t i ve X c o n t ro l s a r e t h e d e sc e n d a n t s o f th e f i r s t O CX c o n t r o l s t h a t a p p e a re d

w he n V i su a l
B a s ic 4 w a s r e le a se d . W h i l e t h e y r e ta i n t h e sa me f i l e e x t e n s i o n , th e y ' r e v e r y
d i f f e re n t
beneath the su rfac e. The origina l OCX con tro ls in cluded a lot of low leve l -

functionality
(and consequently had to support many COM interfaces), and therefore were
heavy and

relatively slow. Th e new Active X controls were specifically rede signed to be


embedded in
HTML pages and delega te much of th eir func tiona lity to the ir conta iner, be it
Microso ft
Internet Explo rer, a Visua l Ba sic form, or any oth er Ac tiveX compliant -

environmen t. Thank s
to this diffe rent approach , Ac tive X controls are gene rally slimmer than old OCXs,
download
faster, and load in memory mo re rapidly .

ActiveX Fundamentals
A c t i v e X C o n t r o l F u n d a m e n t a l s

V i s ua l Ba s i c 5 a n d 6 g i v e y o u a l l t h e to o l s y o u n e e d t o c re a t e p o w e r f u l A c t i v e X
c on t r o l s ,
w h ic h y o u c a n th e n re u se in a l l y o u r p ro j e c t s . M o r e p r e c i se l y , y o u c a n c re a te t wo
d i f f e re n t
types of Ac tiveX con tro ls:
q

P r i v a t e Ac t i v e X c o n t ro l s t h a t c a n b e i n c lu d e d i n a n y ty p e o f V is u a l Ba s i c p ro j e c t .
T h ey ' r e sa v e d i n f i l e s w i t h t h e
. ctl
e x te ns i o n , a n d y o u c a n re u se t h e m in a n y o th e r V i s u a l Ba s i c p ro j e c t me r e ly b y
a dd i ng t h e f i l e t o t h e p ro j e c t . ( T h i s
i s re us e a t t h e so u rc e c o d e l e v e l . )
q
-

Public ActiveX controls that can be included only in ActiveX control projects; you
have to compile them into OCX files, and then you can use them in any other
Microsoft Windows application written in Visual Basic, Microsoft Visual C+ + , or
any other development environment that supports ActiveX controls. (This is reuse at
the binary level.)

V i s ua l Ba s i c 5 wa s t h e f i r s t l a n g u a g e t h a t p e r m i t t e d p r o g r a m m e rs to c r e a t e
A c t i ve X c o n t ro l s u s i n g a v i su a l a p p ro a c h . As y o u ' l l se e i n a mo me n t , y o u c a n
c re a t e p o w e r f u l c o n t r o l s b y s i mp l y g ro u p i n g s im p l e r c o n t ro l s t o g e t h e r : Th e se
c on t r o l s a r e k n o w n a s constituen t controls .

By putting toge ther a Pic tu reBox and


two scro ll bar con tro ls, fo r example , you can cre ate an Ac tiveX con tro l tha t can
scroll its conten ts. Visual Basic a lso allows you to crea te an Ac tiv eX control
without using any constituen t con tro ls. The se are the so calledo w n e r d r a w n
- -

ActiveX controls.

You should also keep this in mind when working with ActiveX controls: You're
used to distinguishing between two
distinct types of people interacting with your program the developer and the -

user. To better understand how ActiveX


controls behave, you need to take another role into account, thea u t h o r of the
control itself. The author's job is to
prepare a control that will be used by the developer to deliver an application to
the user. As you'll see, the author's and
developer's perspectives are sometimes different, even though the two roles
might be occupied by the same person.
(That is, you might act as the author of the control and then as the developer
who uses it.)

A dd a n e w A c t i v e X c o n t r o l p r o j e c t to th e e n v i ro n me n t . T h i s n e w p ro j e c t a l re a d y
i n c lu de s a U se r C o n t r o l mo d u le .
( A l t e r na t i v e ly , ma n u a l ly a d d a U s e r Co n t ro l mo d u l e f ro m t h e P r o je c t m e n u i f
y ou ' re c r e a t i n g a P r i v a t e A c t i v e X
control.)
2 . G iv e t h e p r o je c t a m e a n in g fu l n a me a n d a d e sc r i p t i o n . T h e f o r me r b e c o m e s t he
n am e of th e c o n t ro l ' s l ib r a ry , a n d t h e l a t te r i s t h e s t r i n g t h a t a p p e a r s i n t h e
C om po n e n t s d i a lo g b o x fo r a l l th e p ro j e c t s th a t u se th i s c o n t r o l . I n t h i s e x a m p l e ,
w e ' l l u s e t h e p r o j e c t ' s n a m e S u p e r TB a n d t h e d e s c r i p t i o n An enhanced TextBox
control .

3. Click on the UserControl designer's window to give it the focus, and then
enter a value for theN a m e property of
t h e c o n t r o l i n t h e P r o p e r t i e s w i n d o w . I n t h i s e x a m p l e , y o u c a n e n t e r Su p e r Te x
t Bo x .

4. Place one or more constituent controls on the surface of the UserControl


designer. In this example, you need to
a dd a L a b e l c o n t ro l a n d a Te x t Bo x c o n t ro l , a s sh o w n i n F i g u re 1 7 1 -