Chapter 8

Using Constituent Controls


CONTENTS

Although previous chapters have demonstrated how to create a control using other controls and how to use constituent controls’ properties, events, and methods in your UserControl, a few issues remain.

You understand how to use constituent controls, but how do you know what controls are available? And if the controls provided by the Windows operating system and Visual Basic donÕt quite fit the bill, where can you turn to uncover more options? This chapter answers those questions.

Inherent Controls

Inherent controls are controls that exist as part of Windows. These controls exist on every user’s machine and are generated in Visual Basic by the VB run-time module you distribute with every VB application you create. Consequently, inherent controls do not have OCX files.

NOTE
If you’re a seasoned Visual Basic veteran but you haven’t used VB in a while, note that the number of inherent controls has increased since the release of Windows 95 and Windows NT 3.5. These controls are the ImageList, ListView, ProgressBar, RichTextBox, Slider, StatusBar, TabStrip, Toolbar, and TreeView controls.

Here is a complete list of inherent controls in Visual Basic 5.0:

The OLE container control does not appear in this list because while you can use an OLE container control in an EXE project, you can’t use the OLE container control as a constituent control in your application, so distributing it is not an issue.

In addition to using inherent controls, you can use OLE controls (known as OCX controls) that predate the current ActiveX specification as constituent controls in your control project. So you can draw on the large market of third-party Visual Basic control vendors to provide constituent controls for your control projects, whether or not the vendor has positioned their controls as being explicitly ActiveX or not. The rule is: if it’s a 32-bit OCX control, it should work in Visual Basic 5.0.

See “Locating Commercial Controls” later in this chapter for information on third-party control vendors.

Acting on All the Constituent Controls in Your Control

Occasionally you may wish to perform an action on all the constituent controls in your UserControl. To do this, you can use a With block with the controls collection. Here is an example of how this works:

  1. Create a UserControl with a number of Label controls, all of different colors. The layout for this control is illustrated in Figure 8.1.

    Figure 8.1 : Multilabel control designer.

  2. Write the following code in the UserControl’s Click event:
    Private Sub UserControl_Click()
        Dim c As Control
        For Each c In UserControl.Controls
            c.Caption = "Changed"
            c.BackColor = &H808000
        Next
    End Sub
    
  3. To test this code, you must place an instance of the control on an EXE project form, run the application, then click on a part of the control’s background (not on one of the labels). You should be able to see the control change to resemble Figure 8.2.

    Figure 8.2 : Multilabel control after clicking.

You can see from this demonstration that you can use a For Each block to iterate through all of the constituent controls in your UserControl. You can use this same technique to apply changes to all the controls in your UserControl no matter how many controls your control project contains.

The code for this example is on the CD-ROM that accompanies this book.

Third-Party Controls

There is no reason why you can’t take existing ActiveX controls and extend their functionality by making them constituent controls of your controls. The problem with doing this is the same problem you face when you use an ActiveX control to begin with: when you use a third-party custom control, you put yourself at the mercy of the developer who created it. Be sure to bear this in mind before adding dozens of third-party controls to your project.

For example, let’s say your control project uses a third-party ActiveX control designed to handle a number of different graphic formats. If you design your project around this control only to discover late in the project that the control doesn’t handle a particular graphic format subtype in quite the way you expected, you’ll be forced to work around the problem or compromise your projectÕs feature set.

Third-party controls represent project dependencies that you have little or no control over. This is the case because with controls you purchase, you usually don’t have access to the control’s source code. And even if you had access to the control’s source code, there’s no guarantee that the control is written in a language you can deal with (many ActiveX controls are written in C++).

NOTE
If you’re considering using a third-party control as a part of your control, you may want to instead consider writing your own code to provide the same functionality. Things that seem impossible to do in Visual Basic, such as multimedia and Internet access, can often be accomplished with calls to the Windows API. For more information on this, see Chapter 11, “Making Windows API and DLL Calls.”

That said, using a third-party control can save you a great deal of development time, particularly if you’re attempting to delve into a complex area of programming.

For example, in Chapter 14, you’ll see an example of how to create a control project that is capable of sending electronic mail over the Internet. In order to do this with code, youÕd need to know not only the Internet SMTP mail protocol, but the Winsock API that enables a Windows application to send and receive data using the TCP/IP networking protocol (an API that is very poorly documented and very difficult to call from a VB perspective).

Using a constituent control resolves all those problems by wrapping them up in a programming paradigm that is easy to understand and consistent.

Licensing Considerations Applicable to Third-Party Controls

Whenever you use a commercial, third-party control in your Visual Basic application, you must have a license to use it. In most cases, an improperly installed or unlicensed ActiveX control won’t be usable in Visual Basic, because the IDE won’t let you instantiate a control unless the appropriate licensing information has been inserted into your computer’s system registry.

But what happens when you purchase an off-the-shelf control from a software vendor, incorporate it into an ActiveX control project of your own, add a few enhancements to it, and redistribute the control to other programmers for their use? Are the other programmers–the users of your new, improved ActiveX control–also required to have a license for the third-party constituent control?

The answer is yes. The reason is simple: when a software company grants you a license to redistribute their ActiveX control, they’re granting you a runtime license, not a design-time license. If you had the right to redistribute a runtime license, the control developer would be giving the control away. There’d be nothing to stop you from writing a wafer-thin wrapper that exposed 100% of the functionality of the constituent control, adding nothing new of your own, and reselling the knockoff version of the control on the street corners of America for $1.98.

NOTE
Special considerations apply to the issue of licensing when you’re sending a control over the Internet. In a nutshell, the license for a control you distribute resides on the Web server, and a temporary copy of the license is passed to each Web browser that encounters it. If your control supports licensing, then users who receive your control through a Web page do not have the ability to use your control at design time, unless they have purchased and installed a licensed copy of your control. For more information on licensing Web-distributed controls, see Chapter 13, “Using Your Control On The Web.” For information on how to add licensing to controls you create, see Chapter 12, ÒDistributing Your Control.Ó

Locating Commercial Controls

There are a number of companies that specialize in selling ActiveX components; many of these companies have been around for years, having built their businesses on the 16-bit VBX market, switching over to OCXes and ActiveX controls as Microsoft goes through its seemingly annual paradigm revisions.

There are also quite a number of free controls out there. The number of freeware and shareware controls available will undoubtedly increase dramatically now that Visual Basic can be used to create controls.

Many sites permit you to download versions of controls for evaluation before you purchase them. Some of these controls are fully functional, others are demo versions that stop working after a period of time or display a message when they aren’t fully licensed.

There are a number of Web sites that make information on ActiveX controls available for free. Here is a listing of the best among them, ranked in descending order of indispensability:

Microsoft

The Microsoft Web site, at http://www.microsoft .com/activex/gallery/, is the first place to look for ActiveX resources.

Figure 8.3 : Microsoft ActiveX Gallery.

As of this writing, the site has over 100 controls available for download. Many of them are geared toward use in Web pages, but they’ll work in VB applications as well.

CNET ActiveX.com

Found at http://www.activex.com, this site is the only ActiveX site to rival Microsoft’s in terms of completeness and ease of navigation. It features a library of downloadable ActiveX controls and Visual Basic utilities divided into categories. It is extremely well-maintained and well-organized.

Figure 8.4 : Activex.com.

The site uses ActiveX controls for navigation, and it serves as a good example of how to use ActiveX to enhance your Web site. (The site works perfectly fine with Netscape as well as Internet Explorer.)

CNET’s activex.com also contains links to news articles on ActiveX technology from CNET’s techie news site, news.com.

TechWeb ActiveXpress

Found at http://www.techweb.com/activexpress/, this site has original articles and tutorials on how to use ActiveX technology on the Web, as well as a large download area with links to controls divided into categories. The full-text search feature is fast and easy to use, displaying only ActiveX-related information.

Figure 8.5 : TechWeb ActiveXpress.

Like the CNET site, this site has a link to a daily news section (http://www.techweb.com/activexpress/xnews/xnews.html), that has a new story on ActiveX-related issues every two or three days.

Dave Central

Who is this Dave person and why does he have a Web site (at http://www.davecentral.com/activex.html) devoted to Microsoft development tools? I couldnÕt figure out the answer to that question, but he does have a pretty decent site.

Figure 8.6 : Dave Central.

The layout isn’t quite as nice as the CNET site, and the number of ActiveX resources isn’t nearly as extensive, but the purpose is pretty much the same: provide a library of downloadable software, mainly focusing on Internet-related tools, including ActiveX controls.

The site lets you do keyword searches to look up information but doesnÕt let you search by category or product. This means that when you do a search on the keyword Òdatabase,Ó for example, you get a long list of database products (rather than a list of Visual Basic database products). This can be a bit inundating, but it’s not impossible to navigate around.

BrowserWatch ActiveX Arena

At the time I hit their page (at http://browserwatch.iworld.com/activex.html), their big list of ActiveX controls (at http://browserwatch.iworld.com/activex/activex-big.html) was formatted a little funny and hard to read in MS Internet Explorer. Also, their search engine is confusing and returned way too much information. I got a few crazy error messages when performing searches, and it never returned anything useful.

Figure 8.7 : BrowserWatch ActiveX Arena.

The site seems fairly comprehensive, though, containing capsule descriptions of controls broken into categories, so if you don’t mind navigating with links instead of a search engine, this is a good resource.

Catalog Resellers Specializing in VB Controls and Add-Ons

There are a number of resellers that specialize in selling Visual Basic add-ons, including ActiveX controls, that can be used as constituent controls in your control projects. Here (in no particular order) is a list of some retailers specializing in VB components.

Programmer’s Paradise

1163 Shrewsbury Avenue
Shrewsbury, NJ 07702-4321
Voice: 800-445-7899
Fax: 908-389-9227
Fax-back (product information): 908-389-8173
Web: http://www.pparadise.com
The Programmer’s Paradise Web site offers online ordering. There are a few products that you can purchase online, then download immediately. They also have a fax-back server that can send information on products to your fax machine.

ActiveXtras

1905 Powers Ferry Road, Suite 100
Atlanta, GA 30339
Voice: 1-800-788-4794
Fax: 1-770-952-6388
Web: http://www.vbxtras.com, http://www.xtras.com/activex
The VBXtras Web site offers online ordering and downloading of controls. They have a great search engine that enables you to search for products by product name, keyword, category, or vendor. They also provide invariably flattering ÒevaluationsÓ of the controls they sell, which can be helpful, I guess, if you’re good at translating from the native Marketingese.

VB only Tools/Zachary Software

1090 Kapp Drive
Clearwater, FL 34625
Voice: 1-800-GO-BASIC
Fax: 813-461-2743
Web: http://www.zaccatalog.com/VBASIC/VBASIC.HTM
E-mail: sales@zaccatalog.com
The VB only Tools Web site permits you to perform keyword searches to find information on products you’re interested in. You can download demo versions of some controls, but as of this writing there was no way to order online. They seem a little too proud of the fact that their Web site is rather rudimentary.

Summary

This chapter delved deeper into the perils of including constituent controls in the controls you develop. The chapter also provided information resources that should help you locate ActiveX resources you can use in your control.

In the next chapter, you’ll look at controls you draw yourself, without any constituent controls.