How to Program
Visual Basic 5.0

Control Creation Edition

by Jeffrey P. McManus


CONTENTS

Introduction

Chapter 1   What's New In Visual Basic 5.0

Chapter 2   Control Basics

Chapter 3   Developing Your First Control

Chapter 4   Control Properties

Chapter 5   Handling and Raising Events

Chapter 6   Control Methods

Chapter 7   Interacting With the Container

Chapter 8   Using Constituent Controls

Chapter 9   User-Drawn Controls

Chapter 10   Window Dressing

Chapter 11   Making Windows API and DLL Calls

Chapter 12   Distributing Your Control

Chapter 13   Deploying Your Control on the Web

Chapter 14   Controls That Interact with the Internet

Chapter 15   Debugging and Error Trapping

Chapter 16   Object-Oriented Programming

Chapter 17   Database Access

Appendix   Regular Expressions in Perl

Chapter 19   Glossary

Credits


HTML conversion by :
    M/s. LeafWriters (India) Pvt. Ltd.
    Website : http://leaf.stpn.soft.net
    e-mail : leafwriters@leaf.stpn.soft.net


PublisherStacy Hiquet
Associate PublisherStephen Sayre
Acquisitions EditorSimon Hayes
Development EditorAngela Allen
Copy EditorCandace Crane
Technical ReviewerVidya Bharat and John Kee
Production EditorsBarbara Dahl and Edith Rex
ProofreaderJeff Barash
Cover Illustration and DesignMegan Gandt
IllustratorMina Reimer
Book Design and LayoutBruce Lundquist
IndexerAnne Leach


Ziff-Davis Press, ZD Press, and the Ziff-Davis Press logo are trademarks or registered trademarks of, and are licensed to Macmillan Computer Publishing USA by Ziff-Davis Publishing Company, New York, New York.

Ziff-Davis Press imprint books are produced on a Macintosh computer system with the following applications: FrameMaker®, Microsoft® Word, QuarkXPress®, Adobe Illustrator®, Adobe Photoshop®, Adobe Streamline, MacLink®Plus, Aldus® FreeHand, Collage Plus.

Ziff-Davis Press, an imprint of
Macmillan Computer Publishing USA
5903 Christie Avenue
Emeryville, CA 94608

Copyright © 1997 by Macmillan Computer Publishing USA. All rights reserved.

PART OF A CONTINUING SERIES

All other product names and services identified throughout this book are trademarks or registered trademarks of their respective companies. They are used throughout this book in editorial fashion only and for the benefit of such companies. No such uses, or the use of any trade name, is intended to convey endorsement or other affiliation with the book.

No part of this publication may be reproduced in any form, or stored in a database or retrieval system, or transmitted or distributed in any form by any means, electronic, mechanical photocopying, recording, or otherwise, without the prior written permission of Macmillan Computer Publishing USA, except as permitted by the Copyright Act of 1976, and the End-User License Agreement at the back of this book, and except that program listings may be entered, stored, and executed in a computer system.

EXCEPT FOR THE LIMITED WARRANTY COVERING THE PHYSICAL CD-ROM PACKAGED WITH THIS BOOK AS PROVIDED IN THE END-USER LICENSE AGREEMENT AT THE BACK OF THIS BOOK, THE INFORMATION AND MATERIAL CONTAINED IN THIS BOOK ARE PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY WARRANTY CONCERNING THE ACCURACY, ADEQUACY, OR COMPLETENESS OF SUCH INFORMATION OR MATERIAL OR THE RESULTS TO BE OBTAINED FROM USING SUCH INFORMATION OR MATERIAL. NEITHER MACMILLAN COMPUTER PUBLISHING USA NOR THE AUTHOR SHALL BE RESPONSIBLE FOR ANY CLAIMS ATTRIBUTABLE TO ERRORS, OMISSIONS, OR OTHER INACCURACIES IN THE INFORMATION OR MATERIAL CONTAINED IN THIS BOOK, AND IN NO EVENT SHALL MACMILLAN COMPUTER PUBLISHING USA OR THE AUTHOR BE LIABLE FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF SUCH INFORMATION OR MATERIAL.

ISBN: 1-56276-485-3

Acknowledgments

I'd like to thank Laura Lemay for her advice and support. Thanks to John Chen for his friendship and for his extremely valuable assistance in helping me navigate the sometimes Byzantine machinations of a certain software company over the years. And thanks to Mike and Laura Medina for their help and advice.

Thanks to my editors at Ziff-Davis Press for believing in me and for letting me keep the corny jokes in.

I'd also like to thank the following legion of truly righteous mental heavyweights for their technical help and hints: Tom Digby, Jim Huguley, Matt Luallen, Tales Normando, Marty Pavlovic, and Bob Rossney. Your generosity with your time and knowledge has improved this book greatly.

This book was written in San Francisco and Livermore, California in Fall of 1996 and Winter of 1997.

This is not a sexist cookbook.


Introduction

A world in which every piece of software has the ability to interact with every other piece of software ain't gonna happen anytime soon. But you can always dream. I personally hope to someday put a software chip in a small house pet-a cat, perhaps-and control it through the use of Visual Basic properties and methods.

But perhaps I'm sharing too much.

Windows developers have had the ability to use software components ever since Visual Basic first came out. People Who Know About These Things tell us that the Windows component market-formerly restricted to VB developers, now expanding to accommodate developers in applications as disparate as FoxPro, Access, Visual C++ and Microsoft Word 97-has grown to a sizable software subindustry that sells on the order of $250 million worth of products. That's a lot of widgets, whatzits, and data-aware ding-dongs by anybody's standards.

How to Hurt Uncooperative Users

With the advent of this kooky Internet thing, it's certain that this component technology will continue to grow and mature and eventually get to the point where it will become a part of every application, Web page, vibrating hotel bed, and house pet on earth.

However, it's not there yet. The idea of sending software components to users through a Web page is a compelling one-if your users are using a Web browser that supports the software component technology standard you chose.

Of course, if visitors to your Web site simply refuse to use "The Correct Browser," you have a number of options for remediation that you may wish to consider:

Only through the cooperation of Webmasters all over the world will the ActiveX assimilation of Planet Earth be completed in a timely and cruelty-free fashion.

Until Web browser support for ActiveX becomes as ubiquitous as the rubber insole, there are still plenty of reasons to develop ActiveX controls. You probably have at least a few of these in mind, or you wouldn't have bought this book (and I wouldn't have written it). It is my opinion that rapid development of ActiveX controls will be most prevalent in two areas: vertical applications (written in Visual Basic or any one of a number of other development environments that support ActiveX) and on private Web servers (also known as intranets).

It will be interesting to see what happens with ActiveX support in Microsoft Office 97, although I couldn't tell you exactly how it's going to work, because as I write this, Office 97 is exactly 24 hours away from being released. (I, for one, am quivering with anticipation.)

Creating Controls in Visual Basic is So Great I Could Just Explode

One of the great unsung promises of ActiveX is that-unlike other cross-platform component technologies, such as Java-you can theoretically build an ActiveX component in any programming language and use it with any programming language.

No longer do VB programmers need to rely on the third-party control market for controls written in C++. In fact, the advent of VB as a control-development environment means that a Visual Basic developer can now turn the tables, providing ActiveX components for developers who create applications in Visual C++. What a concept.

It's likely that more languages and development environments will support the creation of ActiveX controls as time goes by. In fact, as this book was being written, a beta version of a new tool for creating ActiveX controls called VisualX from InterGroup Technologies was released. It's not quite as good as Visual Basic's Control Creation Edition (for reasons I'll let you discover on your own), but it is worth checking out. You can download it for free from http://www.intergrouptech.com/form.html.

Why Johnny Can't Create Controls

Visual Basic programmers have been whining for years about their inability to create ActiveX controls in Visual Basic. "How hard could it be to include this ability in VB?" they ask. Well, the Visual Basic gnomes at Microsoft listened. And they came up with a bonus: In addition to giving you a way to create ActiveX controls in Visual Basic, they made it a free download from the Microsoft Web site.

Because it's so easy to create controls using Visual Basic (easy, that is, once you've purchased and read the informative tome you now hold in your hands), it's likely that we'll begin to see a number of new, interesting, fascinating, and perhaps disgusting contributions to the world of ActiveX controls in coming years.

So, there it is. You have before you the Visual Basic Control Creation Edition. Please use its powers only for good, never for evil.

This Is Not What This Book Is About

Now that you have an idea about what this book is about, I'll go into some of the things it isn't about. (By the way, I always hate it when authors say "er, um, this topic is beyond the scope of this book." It seems like such a cop-out. So when a topic is beyond the scope of this book, I try to give you a reference to a book where the topic is covered in more depth.)

This book does not delve into the technology initiatives behind ActiveX controls such as COM, DCOM, and OLE, except where they are necessary to convey points directly related to building controls in Visual Basic. I did this by design; you don't need to be steeped in these initiatives in order to create good controls any more than you need to know everything about the Windows API to create good Visual Basic applications. If you want information on the forest rather than the trees, I'll refer to you the documentation and tools available in the ActiveX Software Development Kit, downloadable from http://www.microsoft.com/activex.

You'll also notice that I conspicuously avoid referring to Java in this book. This isn't because I don't have anything to say about ActiveX versus Java; it's more a function of the fact that I didn't want to muddle the instructional material in this book with pointless factoids on this alleged clash of the technology titans. My opinion on the whole ActiveX versus Java hubbub, for what it's worth, is this: Viewing ActiveX and Java as a winner-take-all competition for domination of the Internet is like viewing lowfat milk versus skimmed milk as a competition for domination of your refrigerator.

To get your very own copy of Visual Basic 5.0 Control Creation Edition for free, go to the Microsoft site at http://www.microsoft.com/vbasic/controls/.

Who Should Use This Book

This book is designed for intermediate to advanced Visual Basic programmers who are interested in creating ActiveX controls in Visual Basic. You may be interested in creating controls that will reside in Web pages, or you may want to create controls that will reside in compiled Visual Basic applications. Either way, this book addresses all of the steps you'll need to take to create a control in VB.

Although this book uses the Control Creation Edition for its examples, you can use any version of Visual Basic 5.0 to create controls. The concepts are the same from one edition of Visual Basic to the next.

You should be aware that this book is not a comprehensive reference to programming in VB. It would, however, serve as an excellent companion to a more general book on VB. If you're a beginner and you want to learn how to create ActiveX controls, it might behoove you to learn how VB works in general before jumping into control creation. You'll save yourself some time and some grief.

Finally, enjoy the book. And don't forget to check out the Web page at http://www.well.com/user/jeffreyp/activex/. I'll try to keep it updated with as much control-creation information and links to VB resources as I have time for.