Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Note, however, that when you select End in the Run menu, Visual Basic unregisters our control with Windows. To register it permanently, use a Windows utility like regsvr32.exe.

Giving ActiveX Controls Persistent Graphics

Visual Basic ActiveX controls are much like Visual Basic forms when it comes to graphics. If you want your control to be automatically redrawn when needed (such as when the program containing it needs to be redrawn), just set the control’s AutoRedraw property to True, just as you would for a Visual Basic form or intrinsic control.

If you prefer not to set AutoRedraw to True (to save memory, for example), you can add your drawing code to the control’s Paint event (just as you can with a Visual Basic form).

Basing An ActiveX Control On An Existing Visual Basic Control

As we saw in the previous topic, you can design your ActiveX controls from scratch, creating the control’s appearance yourself. However, you can also base your ActiveX on an existing Visual Basic control.

Let’s see an example to make this clearer. In this case, we’ll base an ActiveX control on a Visual Basic text box. Create a new ActiveX control now by selecting the ActiveX control item in the New Project dialog box. Now draw a text box, Text1, in the control (its size and position don’t matter because we’ll set that ourselves when the control runs); add this new text box just as you would any text box to a form—just use the Text Box tool. Now set the text box’s ScrollBars property to Both (3), and set its MultiLine property to True.

When the control is first displayed, we stretch the text box to cover the control this way in the ActiveX control Initialize event handler:

Private Sub UserControl_Initialize()
     Text1.Left = 0
     Text1.Top = 0
     Text1.Width = ScaleWidth
     Text1.Height = ScaleHeight
End Sub

That’s it—now select the Start item in the Run menu, click on OK if the Project Properties dialog box appears (it only appears when you run the control for the first time) to open the control in the Internet Explorer, as shown in Figure 20.5. You can type text into the text box, as also shown in Figure 20.5.


Figure 20.5  Basing an ActiveX control on a text box.

As you can see in Figure 20.5, our ActiveX control displays a text box complete with scroll bars, ready for use. We’ve based our ActiveX control on an existing Visual Basic control.

Of course, as it stands, our ActiveX control is exactly like a Visual Basic text box, so it’s hard to see why you’d make an ActiveX control out of it. However, you can tailor that control the way you want it—for example, you can do a lot of processing behind the scenes on the text in the text box (just as you could in any Visual Basic program). You can also change the way the control interacts with the user, as we’ll see in the next topic.

Handling Constituent Control Events In An ActiveX Control

The Aesthetic Design Department is calling. It’s nearly Christmas and they’ve come up with a great idea for a new ActiveX control based on Visual Basic text box. This new Christmas text box control will not display the letter “L”—no “L”, get it? Oh, you say. They ask, when can you have it ready?

The controls you use in an ActiveX control are called constituent controls, and you can handle events from those controls just as you would in a standard Visual Basic program. Let’s see an example. Here, we’ll set up the no “L” text box. Create a new ActiveX control by selecting the ActiveX Control item in the New Project dialog box. Now draw a text box, Text1, in the control (its size and position don’t matter because we’ll set that ourselves when the control runs); add this new text box just as you would any text box to a form—just use the Text Box tool. Now set the text box’s ScrollBars property to Both (3), and set its MultiLine property to True.

Add this code to the Initialize event handler to stretch the text box over the entire ActiveX control when the control first appears:

Private Sub UserControl_Initialize()
     Text1.Left = 0
     Text1.Top = 0
     Text1.Width = ScaleWidth
     Text1.Height = ScaleHeight
End Sub

Now add a KeyPress event handler to the text box in the same way as you would in a standard Visual Basic program (that is, using the drop-down list boxes in the code window):

Private Sub Text1_KeyPress(KeyAscii As Integer)

End Sub

Here, we’ll just watch for the letter “L”:

Private Sub Text1_KeyPress(KeyAscii As Integer)
     If KeyAscii = Asc("L") Then
...
     End If
End Sub

If the user does press the letter “L”, we can cancel that letter by setting the KeyAcsii argument to 0, and we can also have the computer beep to indicate that we’ve canceled this letter:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = Asc("L") Then
         KeyAscii = 0
         Beep
    End If
End Sub

And that’s it—now this ActiveX control will display a text box, but it won’t let the user type the letter “L”. In this way, we’ve handled an event from a constituent control in our ActiveX control.

The code for this example is located in the activextextcontrol folder on this book’s accompanying CD-ROM.

Adding Controls To An ActiveX Control (A Calculator ActiveX Control)

In the previous topics, we’ve seen how to design an ActiveX control’s appearance from scratch and how to base an ActiveX control on a Visual Basic control (or other ActiveX controls). However, ActiveX controls frequently contain more than one constituent control. For example, what if you wanted to create a calculator ActiveX control? How would you design that control in Visual Basic?

Let’s see how this works; here we’ll create an ActiveX control that is really a calculator that the user can use to add numbers. Create a new ActiveX project now, giving it the project name “calculator” (use the Project1 Properties item in the Project menu to open the Project Properties dialog box and enter “calculator” in the Project Name box). In addition, change the name of the control itself from UserControl1 to CalculatorControl in the Properties window (just set the control’s Name property). Now when we add this control to another program using the Components dialog box, this control will be listed as “calculator”, and when we create controls of this type in that program, the first one will be called CalculatorControl1, the next CalculatorControl2, and so on.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.