Look in a guidestore and you'll find many thick guides that discuss ActiveX. ActiveX is Microsoft's new open technology that is supposed to merge the desktop with the Internet seamlessly. Although that lofty goal is probably thrown around too lightly today, it's true that ActiveX takes component technology a step forward. Although Visual Basic programmers have been used to drop-in controls since Visual Basic's version 1.0, ActiveX controls give programmers on all PC development systems similar abilities and the controls can communicate with each other and with applications without regard for the development language being used.
This lesson, being only an hour long, can only expose the tip of the ActiveX iceberg. Nevertheless, by the time you finish this lesson, you will have a better idea of what ActiveX is, how Visual Basic supports ActiveX, and how ActiveX takes its predecessors, OLE and custom controls, to their next step.
The highlights of this hour include
In Hour 12, "Dialog Box Basics," you learned how to add the Common Dialog Box control to Visual Basic's Toolbox window. In Hour 19, "Toolbars and More Graphics," you added additional tools to produce a toolbar and an image list. The tools that you added are examples of ActiveX controls.
New Term: An ActiveX control is a control you can add to Visual Basic's Toolbox window. If the Toolbox window does not contain the control you need, you might find an ActiveX control that suits your purpose and you can add that control to the Toolbox window for use in your application.
All the controls you find in Visual Basic's Project | Components dialog box (shown
in Figure 21.1) are ActiveX controls. Search through the dialog box now to locate
controls that interest you. You may never use all of the controls, but some you'll
use many times (such as the Toolbar control).
Figure 21.1. Visual Basic comes with many ActiveX controls.
As you look through the controls, you'll find a Marquee control that displays
moving text across a form, a Calendar control, a Charting control, a Modem communications
control, and several others, including some Internet controls you'll read about in
this guide's final lesson. By the way, all the Internet controls also work for intranets,
the intracompany networked Internet connections so much in use today. When you add
many of the controls, such as the marquee control, to your Toolbox window, you can
probably figure out which properties to set from the Properties window, but the majority
of the controls support too many esoteric properties, events, and methods for you
to figure all of them out without help. Visual Basic's guides Online reference describes
these additional ActiveX controls so you can get help with a control when you need
help. The Components dialog box is not the only place you'll find ActiveX controls.
If you click the Component dialog box's Browse command button, you can search your
hard disk for other controls. If, for example, you subscribe to the Microsoft Network
online service, your Microsoft Network folder will contain some ActiveX controls
you can use. Many vendors sell ActiveX controls and you can search Microsoft's Web
site for additional information. Many online services and Internet pages offer free
or shareware ActiveX controls that you might want to try as well. Search the Internet
using some of the search engines available for a list of ActiveX sites.
Previous versions of Visual Basic supported these extra controls, but Visual Basic used to work only within a 16-bit environment. Therefore, the tools the Visual Basic programmers used were 16-bit tools called VB custom controls. A custom control, therefore, was a control you added to your Visual Basic Toolbox window to gain additional power.
As the need for tools grew and as other programming platforms, such as Visual C++, began requiring such extra tools, these other platforms began supporting the use of VB custom controls. Therefore, if a Visual C++ programmer wants a Text Box control, the Visual C++ programmer had to locate a Visual Basic Text Box control file and add the text box to Visual C++'s development environment. (Those C++ programmers are always playing catch-up to Visual Basic programmers!)
NOTE: The 16-bit VB custom controls use .VBX for their filename extensions. Visual Basic version 5 can no longer use these 16-bit controls because version 5 supports only 32-bit controls.
New Term: Encapsulation refers to a package of data and code that works like a small program. A control is encapsulated.
Soon, shortcomings of the VB custom controls began surfacing and their capability for taking advantage of new technology, such as 32-bit operating environments, became obvious. Microsoft developed a new control standard called OCX controls. One of the nice things about VB custom controls was their capability to work between and inside several programs even if the programs that used them were not Visual Basic programs. The controls were encapsulated so that the programming language only needed to know the properties, methods, and events supported by the controls to use the controls. The OCX controls, called that because of their .OCX filename extensions, kept all of the 16-bit controls' advantages but also worked inside the 32-bit environment.
New Term: OLE (short for object linking and embedding) refers to the process of inserting linked and embedded objects in one application that another application created.
All along the way, the distinction between OLE and these OCX controls became blurred. An OLE process used a custom control to do its job and the OCX controls further refined the OLE process so that a programmer could embed a complete application written in Visual Basic inside a Visual C++ program. In addition, the user could even drag an Excel worksheet into a Word document, and that worksheet not only became another data item inside the document, but the worksheet was active; when the user clicked the worksheet Excel's menus appeared in place of Word's. That Excel worksheet was nothing more than an advanced OCX control.
ActiveX controls are OCX controls that take these drag-and-drop and drop-into-code concepts even further. An ActiveX control can appear on a Web page (if the page is ActiveX enabled and the user's browser is also; most Web browsers are ActiveX enabled today) for anyone to use. In other words, if a Web page contains an ActiveX control, even if that control is a complete Visual Basic application turned into an ActiveX control (no size limitation for controls exists), the users who view the Web page see the application and interact with it as if they were running it from their own hard disk. ActiveX controls took the concept of OCX controls to the Internet. Now, if you want a special tool such as a command button on your Web page, you can just place an ActiveX command button control on the Web page during the page's development, and your page's users will be able to click the command button.
NOTE: Microsoft seems to be making a push for all code to be these kinds of ActiveX controls. Future operating systems are supposed to be ActiveX based. All programs will, in effect, be ActiveX controls. Therefore, you can embed any application within any other and borrow technology instead of reinvent it. Future programming, in theory, will involve building and combining prewritten ActiveX controls.
Don't throw out your Visual Basic programming language skills just yet, however. The ActiveX control as a total solution is still theory and is only partially available and working today in reality. Your Visual Basic skills are not only going to be needed in the future as ActiveX controls gather steam, but your Visual Basic programming skills are going to be needed even more as companies retool their applications and turn applications into such controls.
The Visual Basic 5 Custom Control Edition lets you build your own ActiveX controls.
If you use Visual Basic's Professional or Enterprise Editions, you not only have Visual Basic, but you also have the ability to create ActiveX controls. Both editions come with the VB 5 Custom Control Edition. Therefore, if you like command buttons but you wish they would support a special event or property your application needs, you can write your own command button control, as an ActiveX control, and then use that control as if Visual Basic came with it. You can add the control to your own application's toolbox (through the Project | Components dialog box) and set its properties from the Properties window.
Here's the really neat thing if you don't use the Professional or Enterprise Edition
but use the Standard Edition: This guide's CD-ROM comes with the complete VB 5 Custom
Control Edition. You'll have exactly the same ActiveX control-building utility that
the other guys have. Professional and Enterprise Edition programmers can enter the
VB 5 Custom Control Edition utility from within their development environment, as
you're about to see. You simply have to exit Visual Basic and start this guide's supplied
VB 5 Custom Control Edition program that you can install right alongside Visual Basic
on your hard disk.
To build an ActiveX control, you must begin by starting the VB 5 Custom Control Edition by creating a new project and selecting the ActiveX Control icon. The VB 5 Custom Control Edition screen looks a lot like Visual Basic's screen, as Figure 21.2 shows.
Figure 21.2. The VB 5 Custom Control Edition screen looks like Visual Basic.
The initial name that the VB 5 Custom Control Edition gives to the control you build is UserControl1; hence the Name property value and the name in the Project window. Most of the tools, windows, and menu objects are exactly the same for the ActiveX control and for your regular Visual Basic session.
Custom controls are tedious to create. Not only must you know Visual Basic and all its language and inner workings (as you do now), but you also need to understand the way ActiveX controls are built and you must understand the wizards available with the VB 5 Custom Control Edition that help you build the controls. Although you'll need to get some fairly heavy training before you learn to build controls with the VB 5 Custom Control Edition, consider the following points:
New Term: Inheritance refers to the capability of object-oriented languages (such as C++) to base new capabilities on existing language capabilities or controls.
NOTE: This lesson cannot possibly describe how to build an ActiveX control in an hour's lesson. Actually, it would take an entire guide to do so. If you want a great reference, check out Teach Yourself ActiveX Control Programming with Visual Basic 5 in 21 Days (Sams.net Publishing). You'll get a glimpse of what's involved from this section, but the guide just mentioned makes you an ActiveX programming superstar.
You can place objects into your application that aren't normally considered to be ActiveX controls. Although you should stick with true ActiveX controls when possible, you can use the Toolbox window's OLE control to drop items from several different applications onto the form window.
For example, suppose you want your user to be able to see a Microsoft Excel worksheet
on your form and interact with the worksheet as if the worksheet were a regular Visual
Basic control. Add the OLE control to your Form window. As soon as you do, the Insert
Object dialog box appears, as shown in Figure 21.3.
Figure 21.3. Adding objects from other applications.
NOTE: As you install Windows applications on your computer, Windows keeps a list, in its Registry, of OLE candidates. Therefore, the Registry contains an entry that tells the system your Paint program's data is available as an OLE object. The list of applications you see in the Insert Object dialog box comes from the Registry's entry.
The Insert Object dialog box gives you the choice of inserting an existing Excel worksheet object (by clicking Create from File) or creating a new object from scratch (by clicking the Create from New option). You will only be able to create objects if you have those applications on your system, but as stated earlier, your Registry knows what is installed, so only those applications appear in the Insert Object dialog box.
If you elect to create the new object from scratch, you can choose that option
and double-click the object type (which, in this example, will be an Excel worksheet).
Visual Basic not only loads a blank object into your OLE control on the Form window,
but all your Visual Basic menus change to Excel's, as shown in Figure 21.4.
Figure 21.4. You can create an Excel worksheet in the middle of the Form window.
Once you create the worksheet, click the Form window outside the worksheet area and you can continue placing the other controls and completing your application. When you finish, run the application to see the worksheet embedded in the form.
NOTE: Although your users will not be able to edit the worksheet automatically, if they double-click the worksheet embedded in the form, an Excel menu will appear across the top of the form and the users can change and enter new values in the worksheet.
Instead of inserting new objects that you must create at design time, you can
insert existing objects such as an Excel worksheet. When you select the Insert Object's
Create from File option, Visual Basic changes the Insert Object dialog box to the
file browsing dialog box shown in Figure 21.5.
Figure 21.5. Selecting a worksheet to insert.
New Term: To link an object means that your application will contain a pointer to the object. If the object ever changes, your application's form will reflect those changes. The object is not stored with your application, but the link to the object is.
New Term: To embed an object means that your application gets a copy of the object. Therefore, if the original object changes, that change will not be reflected in your application until you or your user make the same change to the application's object. The object is stored with your application so if something happens to the original, there exists no link to be broken.
The Link option informs Visual Basic that you want to link the new object and not embed it. You can select to choose Link or not depending on how current the object must be with the original object's file.
TIP: Click the Icon option if you want the object to appear as an icon on your form when the user runs the application. If you don't click this option, the object (in this case an Excel worksheet) appears on your Form window as a small worksheet.
When the user runs your application, the user can double-click the worksheet (or the icon) to add Excel menus to the Form window and to change the worksheet.
New Term: An ActiveX container is an application, such as Internet Explorer, that can display and activate ActiveX documents.
ActiveX documents are difficult objects to create from scratch. An ActiveX document
must be contained within an ActiveX container application such as Internet Explorer.
If you have Internet Explorer, try this: Start Internet Explorer but don't log on
to the Internet as you might normally do. Open a Word document. If you've never tried
this, you might be surprised at the results. Internet Explorer can display the Word
document, formatted completely, and you can edit the document as if it were shown
inside Word. Figure 21.6 shows such a document embedded inside Internet Explorer.
Figure 21.6. Internet Explorer is an ActiveX container program.
You have access to Word's menus inside the ActiveX container. Also, you can right-click over the text to see Word's pop-up menu; misspelled, foreign, and abbreviated words are underlined as possible misspellings; and you can highlight and format text by pressing Word's typical formatting keystrokes (such as Ctrl+B to boldface text).
New Term: When an ActiveX container activates an ActiveX document, all the document's usual controls and features become available.
A Word document is an ActiveX document. An ActiveX container, such as Internet Explorer, can display and let you work within the ActiveX document. ActiveX documents are going to become more important as the Internet becomes more important. The more you work within a Web browser, the more likely it will be that you'll want to view data from another source, such as a Word document. When you're working with an ActiveX document, you don't have to start Word to read the document.
Visual Basic's Professional and Enterprise Editions include a wizard that converts your forms to ActiveX documents. Although the wizard cannot convert complete applications to ActiveX documents, you can convert forms with all their features.
WARNING: Surprisingly, if your form contains OLE controls, even ActiveX-based OLE controls, those controls do not convert to the ActiveX document.
To run the wizard, called the VB ActiveX Document Migration Wizard, you must add the wizard to your Add-Ins menu by following these steps:
Figure 21.7. Adding the wizard to the Add-In Manager dialog box.
Do you remember the animated form with the happy face moving up the form from
Hour 18, "The Graphic Image Controls"? If so, you'll enjoy seeing it again,
only this time as the ActiveX document in Figure 21.8.
Figure 21.8. You can convert any form to an ActiveX document!
TIP: You might use Microsoft Office, which is the most popular desktop suite in use today; the Office Binder application is little more than an ActiveX container. Therefore, Office users will be able to see your ActiveX documents and work with the controls contained in those documents even if those users don't use the Internet.
You've now been introduced to a whirlwind tour of ActiveX. This hour summarizes ActiveX and how ActiveX fits in with Visual Basic programming. ActiveX controls are becoming more important as Internet-based usage grows because of the strong interaction ActiveX controls have with ActiveX-enabled Web browsers.
The next hour covers another fairly advanced issue: Visual Basic objects. By learning how to work with objects through code, you will be able to increase the work your applications do.
The quiz questions and exercises are provided for your further understanding. See Appendix C, "Answers," for answers.