310 likes | 443 Views
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
E N D
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
ActiveX 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
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
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
Combining 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
Example: Address Control • File>New Project>ActiveX 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
Address 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
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
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 code Print UserControl11.AddressText 'might help to check/copy control 'name on Form1 Properties Window • run project 'Notes
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 browser • set the ActiveX Control project (Project1.vbp) as the StartUp, then run
Compiling • Open ActiveX control project (not group) • File>Make [file].ocx • save 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
Testing Compiled Control • start a standard EXE • add your new ocx to Toolbox with Project> Components> Browse> 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
Enhancing 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
Creating an Enhanced Control • File>New Project>ActiveX Control • make project name = TextP • right click Project 1>Properties>Project 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
Adding 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
Compiling • File>Make [file].ocx • save 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
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> OK • click/drag new control onto form for every input that needs to have first letter capitalized (on all words) • add regular textboxes otherwise
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
Control Interface Wizard • comes with VB, automates some of code creation for custom controls • Add-Ins>Add-In Manager>Visual Component Manager 6.0 • needs to say Startup/Loaded on right side • may need to double click it to get it right
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)
Control Interface Wizard - 3 • Project>Add User Control>VB ActiveX Control Interface Wizard, Next • select, add (>) Text property, Next • New CharAccept (Property) OK • New UserError (Event) OK, Next
Control Interface Wizard - 4 • Set Mapping • (Public Name) click Text (on left) • (Control) click txtCharLimit (on right) • (Member) Text (on right) • Next
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)
Control 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
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
THE END • following slides have not been completed yet
Property Pages Wizard • also comes with VB, automates setting properties for custom controls • unload previous standard EXE • 1st way to start • Project>References>VB Property Page Wizard>OK • Project>Add Property Page>VB Property Page Wizard>Open
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
Property Pages Wizard - 3 • Add-Ins>ActiveX Property Page Wizard, Next • click Add, rename new page as General, OK, Next • select CharAccept, Next, Finish
Property 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