|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Adding A Property To An ActiveX ControlWhen we add our new custom calculator ActiveX control to a Visual Basic program, as we did in the previous topic, you can see that the control already has quite a number of controls built into it, as shown in the Properties window in Figure 20.12. Besides that standard set of properties (called ambient properties and stored in AmbientProperties objects, which you get from the Ambient property), we can also add our own properties to an ActiveX control (stored in Extender objects, which you get from the Extender property).
Lets see an example. Here, well add two properties to the calculator ActiveX control, Operand1 and Operand2, which will hold the text in the top two text boxes in the calculator, operand 1 and operand 2, which we add when the user clicks the = button. After we add those properties, you can access them in programs that make use of our ActiveX control just as you would any other ActiveX controls properties. Heres an example where we set operand 1 in the calculator after you add a calculator control, CalculatorControl1, to a standard Visual Basic program; as you can see, this is just like setting the text in a text box:
Private Sub Command1_Click()
CalculatorControl1.Operand1 = 100/
End Sub
To add the Operand1 and Operand2 properties to the calculator ActiveX project, open that project in Visual Basic, open the code window, and select the Add Procedure item in the Tools menu. This opens the Add Procedure box, as shown in Figure 20.13.
Put the name of the first property, Operand1, in the Name box, and select the option button labeled Property, then click on OK. Doing so creates two procedures, a Let and Get procedure for the property: Public Property Get Operand1() As Variant End Property Public Property Let Operand1(ByVal vNewValue As Variant) End Property In the same way, add the Operand2 property, creating the procedures for that property as well: Public Property Get Operand2() As Variant End Property Public Property Let Operand2(ByVal vNewValue As Variant) End Property When Operand1 is set in code or by Visual Basic, the Operand1 Let procedure is called; when other code or Visual Basic wants to retrieve the current value of Operand1, the Operand1 Get procedure is called. That means that we return the text in the two text boxes corresponding to the calculators operands in the Get procedures:
Public Property Get Operand1() As Variant
Operand1 = Text1.Text
End Property
Public Property Get Operand2() As Variant
Operand2 = Text2.Text
End Property
And we set the text in the operand text boxes from the value passed to us in the Let procedures:
Public Property Let Operand1(ByVal vNewValue As Variant)
Text1.Text = vNewValue
End Property
Public Property Let Operand2(ByVal vNewValue As Variant)
Text2.Text = vNewValue
End Property
Thats all it takes. Now when you create a control of this type in another Visual Basic program, youll see that the new control has two new properties: Operand1 and Operand2. You can set those properties in the Properties window at design time, as shown in Figure 20.14, or in code:
Private Sub Command1_Click()
CalculatorControl1.Operand1 = 100
End Sub
However, there is a problem. When you set the Operand1 and Operand2 properties at design time in a program that has a calculator control embedded in it and then run the program, nothing appears in the operand text boxes. Why not? It turns out that we have to make the properties persistent if we want to let the user set them at design time, and well look into that in the next topic. The code for this example, activexcalculatorcontrol.ctl version 1 (version 2 will have persistent properties), appears in Listing 20.1.
Listing 20.1 activexcalculatorcontrol.ctl version 1
VERSION 6.00
Begin VB.UserControl CalculatorControl
ClientHeight = 3600
ClientLeft = 0
ClientTop = 0
ClientWidth = 4500
ScaleHeight = 3600
ScaleWidth = 4500
Begin VB.TextBox Text2
Height = 495
Left = 1680
TabIndex = 4
Top = 1200
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "="
Height = 495
Left = 1680
TabIndex = 3
Top = 1920
Width = 1215
End
Begin VB.TextBox Text3
Height = 495
Left = 1680
TabIndex = 1
Top = 2640
Width = 1215
End
Begin VB.TextBox Text1
Height = 495
Left = 1680
TabIndex = 0
Top = 300
Width = 1215
End
Begin VB.Label Label1
Caption = "+"
Height = 255
Left = 2160
TabIndex = 2
Top = 840
Width = 375
End
End
Attribute VB_Name = "CalculatorControl"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Private Sub Command1_Click()
Text3.Text = Str(Val(Text1.Text) + Val(Text2.Text))
End Sub
Public Property Get Operand1() As Variant
Operand1 = Text1.Text
End Property
Public Property Let Operand1(ByVal vNewValue As Variant)
Text1.Text = vNewValue
End Property
Public Property Get Operand2() As Variant
Operand2 = Text2.Text
End Property
Public Property Let Operand2(ByVal vNewValue As Variant)
Text2.Text = vNewValue
End Property
|
|
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. |