| September 23, 1999
Creating Visual Basic Custom Controls
by Trevor Edis
Many programmers do not look at programming as a modular process. Instead, they jump into a project without considering that some of the code they’re creating will be used in future projects.
There are many ways to create a modular structure for your work, such as using class modules, etc., but, we will discuss reusable custom controls, which can be very useful time savers.
These programmable elements are called ActiveX controls. They are the same as the built-in controls in Visual Basic, such as the textbox or picture controls. They used to be called OLE controls, OCXs, or OLE custom controls. I think that Microsoft wanted a more updated and catchy name, and ActiveX is what they came up with.
These controls incorporate events, such as mouse clicks, and they can be built into other controls. The file extension is .ocx.
In order to demonstrate how to build and use an ActiveX control, we will go through the process of building a scrolling text control. The control is placed on our form and activated. This article covers the basics, but I invite you to experiment so that you can create a control that best suits your needs.
‘Assign variable with local scope
Const Caption = “Scrolling Text”
Private Sub Timer1_Timer()
Once the custom control is complete, it needs to be compiled into an OCX. file. This is done by clicking on file menu and then choosing ‘Make project ocx’ where project is whatever you called this project. After the control has been compiled, it can be used in any Visual Basic project. All you will need to do is start or open a project the way you normally would, then right click on the toolbox and choose components. Use Browse to locate the control and then choose OK. An icon representing the control is now display on the toolbox. This is the same image that you chose earlier. Select it, and draw the control onto your form just as you would with any other control. Run the project. That’s it, you’re first custom control is up and running.
There can be a lot more to controls than what we’ve covered here, but this is a good start. They can have their own property pages that have dialog boxes where properties can be set at design time. Also, ActiveX controls can be used on the Internet to provide users with a richer experience. Web pages with custom controls are much more difficult for the casual person to copy, making them a good choice. You can setup controls that add functionality to your projects such as FTP, a browser, e-mail, calendar, resize control, data base interaction, etc. The list is virtually limitless. There are many companies that build and sell controls, take a look at what features their controls have and try to build one of your own that better suits your particular needs.
In the future, try to identify areas of your programming projects that will be repeated. Spending a little extra time to create reusable controls will save you time and money down the road.
About the author:Trevor Edis is a college computer instructor who has been working with computers for the past 15 years. He has gained a reputation as a specialist in Visual Basic and has taught at several colleges. He has developed many business solutions for a variety of companies locally and internationally.