Chapter 5. Building Your Own VB Control

One of the great strengths of VB is its powerful visual builder (IDE) environment. It is easy to build complex and sophisticated user interfaces by just dragging a few components onto a form and writing a little code to control their interactions. However, if no control does exactly what you want, it appears at first to be quite difficult (or impossible) to create a new control with these new properties. In this chapter we carry the idea of OO programming a little further by showing how easy it is to derive a new ActiveX control from the existing ones in VB6. We'll show you how to do this in VB.NET in Chapter 7.

A Highlighted Text Field

Suppose we would like to build a text entry field that always highlights all the text when it receives the focus. This can be desirable whenever you want to make sure that a single key press will replace the previous text with new text. In fact, it seems that what we want to do is derive a new class from the TextBox. However, VB6 doesn't allow us to do this directly because it doesn't support inheritance.

However, you'll soon discover that the Gang of Four's maxim applies here.

Favor object composition over inheritance.

And object composition is just another word for encapsulation or containment. Thus, if we can create a new class that contains the TextBox but highlights the text whenever the control gets the focus, we'll have what we want.

We'll start by using the VB IDE to create the ActiveX control. Select File | New Project and select ActiveX Control from the menu, as shown in Figure 5-1.

Selecting ActiveX Control creation from the VB Project menu

Figure 5-1. Selecting ActiveX Control creation from the VB Project menu

This brings up a gray form without borders called a UserControl that provides the canvas on which to create your control, as shown in Figure 5-2.

The UserControl canvas

Figure 5-2. The UserControl canvas

First, change the name from UserControl1 (which is hardly mnemonic) to HiText by pressing F4 and changing the name in the Properties window. Then, drop a TextBox onto the form in the upper left corner, and resize the gray background to match the size of the text box, as illustrated in Figure 5-3.

The Text box inside the UserControl

Figure 5-3. The Text box inside the UserControl

Now, let's add just a little code. Select the GotFocus event for the txEntry box you just added and add the code.

Private Sub txEntry_GotFocus()
Dim s As String
       s = Text1.Text
       txEntry.SelStart = 0          'Start highlight
       txEntry.SelLength = Len(s)    'end highlight
End Sub

Resizing a User Control

The last important part of a user control is that it must resize during the design mode. Select the HiText UserControl, and select the Resize event. Enter the following code.

Private Sub UserControl_Resize()
 txEntry.Width = Width     'design time resize of width
 txEntry.Height = Height   'and height
End Sub

Testing Your HiText Controls

Now, to test this control, close its design window. Then, select File | Add project to add a second project with which to test your new control. On the Controls toolbar, you will find a new icon representing the HiText control, as shown in Figure 5-4.

The HiText control icon in the Controls toolbar

Figure 5-4. The HiText control icon in the Controls toolbar

This icon will only be active if the design window is closed for the HiText control. Click on this new icon, and put an instance of the control onto the Form panel of the new project you just created. Then add a button labeled Clear, as shown in Figure 5-5.

The test project for our HiText control

Figure 5-5. The test project for our HiText control

If you try to resize the HiText control, you'll see that the included TextBox resizes with it in design mode.

Now, if you run the test form, you will be able to type text into the HiText box. Then, if you press the Tab key twice, you will move the focus down to the button and back to the HiText control. When it receives the focus, it will display the text highlighted, as shown in Figure 5-6.

The HiText control in action

Figure 5-6. The HiText control in action

Adding Properties and Methods to User Controls

You can add any property you want to your new user control, and these properties will appear in the Properties box if you create both a Let and a Get property. For example, you might want to be able to change the Backcolor of the TextBox. Just add the following code.

Property Let Backcolor(c As ColorConstants)
  txEntry.Backcolor = c
  PropertyChanged "BackColor"
End Property
'------
Property Get Backcolor() As ColorConstants
  Backcolor = Text1.Backcolor
End Property

Note that you must add the PropertyChanged method call whenever you change a property of a user control. This passes this information to theVB engine to make sure that the screen is refreshed as needed.

In the same way, you can add methods to your user control, and they will appear in the syntax completion drop-down for any instance of that control. For example, the TextBox control lacks a Clear method. However, we can easily add one to our new control.

Public Sub Clear()
  txEntry.Text = ""
End Sub

Now that we've added that convenient Clear method, we can connect it to the Clear button.

Private Sub Clearit_Click()
  hiText1.Clear
End Sub

And clicking the button will clear our new text box.

Compiling a User Control

Once you have your test program and user control working, you can compile both of them by selecting File | Make Project Group. This will produce an .EXE file for your test program and an .OCX file for your user control. Then, if you want to use this control in further programs, you will find that VB has automatically registered it, and you can find it under Project | Components to add to any new project.

Summary

Building a user control like this helps you see how encapsulation can be used to create new objects that have the properties you need. The only disadvantage of this approach is that you must add all of the properties to the control manually rather than having them inherited, as could occur in VB7, which allows inher itance. However, in many cases encapsulation is very effective, since you have only a few properties to pass through from the outer control interface to the enclosed control interfaces. And if the new control contains more than one basic control, this is the only possible approach.

Programs on the CD-ROM

ActiveXhtx.vbg Highlight control project group for both DLL and example
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset