Title: CIS 338: Creating ActiveX Controls
1CIS 338 Creating ActiveX Controls
- Dr. Ralph D. Westfall
- March, 2003
2ActiveX Background
- important part of Microsoft's DNA
- Distributed interNet Applications
- software components delivered from network where
and when needed - COM (component object model) makes it possible
for individual components to work together
3ActiveX Includes
- scripting on client, or on server (ASP)
- controls used locally or in web pages
- ActiveX documents browser can show Office
files Word, Excel, etc. - ADO provides way to get data
- server components interface IIS to other data
sources
4What Are ActiveX Controls?
- like other VB controls text box, list box, data
control, etc. - may be created by third parties, rather than
Microsoft - other software companies
- individual programmers people like you
- you could sell through third party vendors
5How Create ActiveX Controls?
- hook together existing VB controls
- added code makes them work together
- modify existing VB controls
- added code changes their behavior
- create a user-drawn control
- VB graphics methods create appearance
- added code provides desired behavior
- combinations of above
6Combining Existing Controls
- get all existing functionality
- "inherit" properties and methods
- handled as a single unit by code
- users understand it easily
- based on previous experiences with individual
components built into the control
7Example Address Control
- FilegtNew ProjectgtActiveX Control
- change project and control names
- add textboxes and labels
- not too far apart
- add appropriate Name properties
- click and drag corners to size the form
- sets Height and Width properties
-
8Address Control - 2
- add some code
- Resize event pp. 309-310 'Notes
- With UserControl 'connect it to
- 'items with dots
- Property procedure p. 310 'Notes
- s s txtStreet vbCrLf
- 'appends string with Cr and Lf
- 'to previous string
9Testing Address Control
- create another project to test it
- FilegtAdd ProjectgtStandard EXE
- note new icon on Toolbox
- close UserControl window
- may need to use Window menu to close
- draw this control on the new form
- test code by resizing the control
10Testing Address Control - 2
- set the standard EXE as startup project
- right click Project 2 in Project Explorer
- note boldface on startup project
- add Command button and code
- Print UserControl11.AddressText
- 'might help to check/copy control
- 'name on Form1 Properties Window
- run project 'Notes
11Testing in Browser
- some ActiveX controls designed for use in web
pages on Internet - inside organizations/Intranets
- if using on the web, need to test in a browser
- set the ActiveX Control project (Project1.vbp) as
the StartUp, then run
12Compiling
- Open ActiveX control project (not group)
- FilegtMake file.ocx
- save the .ocx file somewhere it will be easy to
find - e.g., in a new folder among your Visual
StudiogtVisual Basic files - unload control project
13Testing Compiled Control
- start a standard EXE
- add your new ocx to Toolbox with Projectgt
Componentsgt Browsegt OK - use new icon on toolbox to add to form
- change StartUp Project to Project 2 (the new
Standard EXE) - add a button and some code for it
- run project
14Enhancing an Existing Control
- alternative to adding same code for desired
behavior in multiple projects - build code into a custom control instead
- can add properties, methods, events to a
control's existing functionality - e.g., add things to a textbox or button
15Creating an Enhanced Control
- FilegtNew ProjectgtActiveX Control
- make project name TextP
- right click Project 1gtPropertiesgtProject Name and
change it - view Properties Window, change (Name) to
something different e.g., TextP1 - add textbox to upper left corner of form
- reduce form width/height to textbox size
16Adding New Behavior
- add a resize event to UserControl
- Private Sub UserControl_Resize()
- Text1.Height UserControl.ScaleHeight
- Text1.Width UserControl.ScaleWidth
- End Sub
- add code for capitalization behavior
- Private Sub Text1_LostFocus()
- Text1 StrConv(Text1, vbProperCase)
- End Sub 'Notes
17Compiling
- FilegtMake file.ocx
- save the .ocx file somewhere it will be easy to
find - e.g., in a new folder among your Visual
StudiogtVisual Basic files - remove ActiveX control project
18Testing Enhanced Control
- create a new project to test it
- FilegtNew ProjectgtStandard EXE
- add your new ocx to Toolbox with Projectgt
Componentsgt Browsegt OK - click/drag new control onto form for every input
that needs to have first letter capitalized (on
all words) - add regular textboxes otherwise
19Testing / Compiling
- test new form
- be sure the new Project is the StartUp form
- if haven't done so already, re-open ActiveX
control and compile it for use in other projects - save the .ocx file in a new folder e.g., among
your Visual StudiogtVisual Basic files
20Control Interface Wizard
- comes with VB, automates some of code creation
for custom controls - Add-InsgtAdd-In ManagergtVisual Component Manager
6.0 - needs to say Startup/Loaded on right side
- may need to double click it to get it right
21Control Interface Wizard - 2
- FilegtNew ProjectgtActiveX Control
- draw textbox on top left corner of form
- reduce form size to size of textbox
- delete Text1 from Text property
- click UserControl
- set UserControl.NametxtCharLimit
- make sure PublicTrue (usual default)
22Control Interface Wizard - 3
- ProjectgtAdd User ControlgtVB ActiveX Control
Interface Wizard, Next - select, add (gt) Text property, Next
- New CharAccept (Property) OK
- New UserError (Event) OK, Next
-
23Control Interface Wizard - 4
- Set Mapping
- (Public Name) click Text (on left)
- (Control) click txtCharLimit (on right)
- (Member) Text (on right)
- Next
24Control Interface Wizard - 5
- Set Attributes
- Public Name CharAccept
- Data Type Integer
- Default Value 0
- Description programmer comments
- Next, Finish
- look at Summary (To Do list)
25Control Interface Wizard - 6
- view code
- note warnings not to delete comments
- used by code generator to work with code the next
time - if don't delete comments, can still use Control
Wizard after adding programmer written code to
generated code
26Control Interface Wizard - 7
- add Limited.Zip code (pp. 329-330 or
www.mcp.com/info ISBN0789715422) - replace code for Let CharAccept
- add code for txtCharSet_KeyPress
- in copied code, need to change every mCharAccept
to m_CharAccept - create standard EXE, add control, run
- standard EXE needs to be start up object
- change CharAccept property of control 1, 2
27THE END
- following slides have not been completed yet
28Property Pages Wizard
- also comes with VB, automates setting properties
for custom controls - unload previous standard EXE
- 1st way to start
- ProjectgtReferencesgtVB Property Page WizardgtOK
- ProjectgtAdd Property PagegtVB Property Page
WizardgtOpen
29Property Pages Wizard - 2
- other way to start
- Add-InsgtAdd-In ManagergtVB 6 Property Page
WizardgtOK - needs to say Loaded may need to double click to
get it right - Add-InsgtProperty Page WizardgtNext
30Property Pages Wizard - 3
- Add-InsgtActiveX Property Page Wizard, Next
- click Add, rename new page as General, OK, Next
- select CharAccept, Next, Finish
31Property Pages Wizard - 3
- create standard EXE, add this control
- click on control
- click Custom in Properties Window
- click General tab
- set CharAccept 1 (or 2)
- run