|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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 GraphicsVisual 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 controls 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 controls Paint event (just as you can with a Visual Basic form). Basing An ActiveX Control On An Existing Visual Basic ControlAs we saw in the previous topic, you can design your ActiveX controls from scratch, creating the controls appearance yourself. However, you can also base your ActiveX on an existing Visual Basic control. Lets see an example to make this clearer. In this case, well 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 dont matter because well set that ourselves when the control runs); add this new text box just as you would any text box to a formjust use the Text Box tool. Now set the text boxs 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
Thats itnow 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.
As you can see in Figure 20.5, our ActiveX control displays a text box complete with scroll bars, ready for use. Weve 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 its hard to see why youd make an ActiveX control out of it. However, you can tailor that control the way you want itfor 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 well see in the next topic. Handling Constituent Control Events In An ActiveX ControlThe Aesthetic Design Department is calling. Its nearly Christmas and theyve 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 Lno 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. Lets see an example. Here, well 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 dont matter because well set that ourselves when the control runs); add this new text box just as you would any text box to a formjust use the Text Box tool. Now set the text boxs 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, well 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 weve canceled this letter:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc("L") Then
KeyAscii = 0
Beep
End If
End Sub
And thats itnow this ActiveX control will display a text box, but it wont let the user type the letter L. In this way, weve handled an event from a constituent control in our ActiveX control. The code for this example is located in the activextextcontrol folder on this books accompanying CD-ROM. Adding Controls To An ActiveX Control (A Calculator ActiveX Control)In the previous topics, weve seen how to design an ActiveX controls 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? Lets see how this works; here well 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 controls 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.
|
|
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. |