Creating Visual Basic Custom Controls 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. 1. 2. 3. 4. 5.
Start a new project. Choose ActiveX control from the New Project dialog box. Add a picture box to the usercontrol - it looks like a form without a border. Add a timer with the interval property set to 50. Rename the picture box 'ScrollBox' and resize it to about 4000 wide and 700 in height. 6. Rename the usercontrol to 'ScrollControl' and resize it so it's just slightly larger than the picture box. 7. Select a bitmap for the 'ToolboxBitmap' property, this will be the image that eventually represents your control on the toolbox. 8. Now we're ready for the code. 'Force declaration of variables Option Explicit 'Assign Private Private Private
variable with local scope TextPosition As Long TextTop As Long TextSize As Long
Const Caption = "Scrolling Text" Private Sub Timer1_Timer()
'check to see if design mode or end user mode If Ambient.UserMode Then ScrollBox.Cls 'if necessary reset text position If (TextPosition + ScrollBox.TextWidth(Caption)) < 0 Then TextPosition = ScrollBox.ScaleWidth End If 'change text starting location and print text TextPosition = TextPosition - 25 ScrollBox.CurrentX = TextPosition ScrollBox.CurrentY = TextTop ScrollBox.Print Caption End If End Sub Private Sub UserControl_Resize() TextPosition = ScrollBox.ScaleWidth 'make usercontrol the same size as scroll box ScrollBox.Left = 0 ScrollBox.Width = UserControl.ScaleWidth ScrollBox.Top = 0 ScrollBox.Height = UserControl.ScaleHeight 'adjust font size according to scroll box height ScrollBox.FontSize = ScrollBox.ScaleY _ (ScrollBox.ScaleHeight * 0.8, vbTwips, vbPoints) TextTop = (ScrollBox.ScaleHeight \ 2) - _ (ScrollBox.TextHeight("A") \ 2) 'check to see if design mode or end user mode If Ambient.UserMode Then Timer1.Enabled = True Else Timer1.Enabled = False End If End Sub Private Sub UserControl_Terminate() 'turn off timer Timer1.Enabled = False End Sub
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.