CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

31
CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003

Transcript of CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Page 1: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

CIS 338: Creating ActiveX Controls

Dr. Ralph D. WestfallMarch, 2003

Page 2: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

ActiveX 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

Page 3: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

ActiveX Includes

scripting on client, or on server (ASP)controls – used locally or in web pagesActiveX documents – browser can show Office files: Word, Excel, etc.ADO – provides way to get dataserver components – interface IIS to other data sources

Page 4: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

What 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

Page 5: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

How 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

Page 6: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Combining Existing Controls

get all existing functionality "inherit" properties and methods

handled as a single unit by codeusers understand it easily based on previous experiences with

individual components built into the control

Page 7: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Example: Address Control

File>New Project>ActiveX Controlchange project and control namesadd textboxes and labels not too far apart add appropriate Name properties

click and drag corners to size the form sets Height and Width properties

Page 8: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Address Control - 2

add some code Resize event – pp. 309-310 'NotesWith UserControl 'connect it to 'items with dots Property procedure – p. 310 'Notess = s & txtStreet & vbCrLf'appends string with Cr and Lf'to previous string

Page 9: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Testing Address Control

create another project to test it File>Add Project>Standard 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

Page 10: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Testing 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 codePrint UserControl11.AddressText'might help to check/copy control 'name on Form1 Properties Window

run project 'Notes

Page 11: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Testing 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 browserset the ActiveX Control project (Project1.vbp) as the StartUp, then run

Page 12: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Compiling

Open ActiveX control project (not group)File>Make [file].ocxsave the .ocx file somewhere it will be easy to find e.g., in a new folder among your

Visual Studio>Visual Basic files

unload control project

Page 13: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Testing Compiled Control

start a standard EXEadd your new ocx to Toolbox with Project> Components> Browse> OKuse new icon on toolbox to add to formchange StartUp Project to Project 2 (the new Standard EXE)add a button and some code for itrun project

Page 14: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Enhancing an Existing Control

alternative to adding same code for desired behavior in multiple projects build code into a custom control

insteadcan add properties, methods, events to a control's existing functionality e.g., add things to a textbox or button

Page 15: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Creating an Enhanced Control

File>New Project>ActiveX Controlmake project name = TextP right click Project 1>Properties>Project

Name and change it view Properties Window, change (Name)

to something different e.g., TextP1add textbox to upper left corner of formreduce form width/height to textbox size

Page 16: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Adding New Behavior

add a resize event to UserControlPrivate Sub UserControl_Resize() Text1.Height = UserControl.ScaleHeight Text1.Width = UserControl.ScaleWidthEnd Sub

add code for capitalization behaviorPrivate Sub Text1_LostFocus() Text1 = StrConv(Text1, vbProperCase)End Sub 'Notes

Page 17: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Compiling

File>Make [file].ocxsave the .ocx file somewhere it will be easy to find e.g., in a new folder among your

Visual Studio>Visual Basic files

remove ActiveX control project

Page 18: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Testing Enhanced Control

create a new project to test it File>New Project>Standard EXE

add your new ocx to Toolbox with Project> Components> Browse> OKclick/drag new control onto form for every input that needs to have first letter capitalized (on all words) add regular textboxes otherwise

Page 19: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Testing / 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 Studio>Visual Basic files

Page 20: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Control Interface Wizard

comes with VB, automates some of code creation for custom controlsAdd-Ins>Add-In Manager>Visual Component Manager 6.0 needs to say Startup/Loaded on right

sidemay need to double click it to get it

right

Page 21: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Control Interface Wizard - 2

File>New Project>ActiveX 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.Name=txtCharLimit make sure Public=True (usual default)

Page 22: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Control Interface Wizard - 3

Project>Add User Control>VB ActiveX Control Interface Wizard, Nextselect, add (>) Text property, NextNew CharAccept (Property) OKNew UserError (Event) OK, Next

Page 23: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Control Interface Wizard - 4

Set Mapping (Public Name) click Text (on left) (Control) click txtCharLimit (on right) (Member) Text (on right) Next

Page 24: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Control 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)

Page 25: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Control Interface Wizard - 6

view codenote 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

Page 26: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Control Interface Wizard - 7

add Limited.Zip code (pp. 329-330 or www.mcp.com/info ISBN=0789715422) 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

Page 27: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

THE END

following slides have not been completed yet

Page 28: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Property Pages Wizard

also comes with VB, automates setting properties for custom controlsunload previous standard EXE1st way to start Project>References>VB Property Page

Wizard>OK Project>Add Property Page>VB Property

Page Wizard>Open

Page 29: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Property Pages Wizard - 2

other way to start Add-Ins>Add-In Manager>VB 6

Property Page Wizard>OK needs to say Loaded: may need to

double click to get it right Add-Ins>Property Page Wizard>Next

Page 30: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Property Pages Wizard - 3

Add-Ins>ActiveX Property Page Wizard, Next click Add, rename new page as

General, OK, Next select CharAccept, Next, Finish

Page 31: CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.

Property Pages Wizard - 3

create standard EXE, add this controlclick on control click Custom in Properties Window click General tab set CharAccept = 1 (or 2) run