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


Adding A Property To An ActiveX Control

When 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).


Figure 20.12  The Visual Basic Properties window.

Let’s see an example. Here, we’ll 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 control’s properties. Here’s 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.


Figure 20.13  Adding a property to an ActiveX control.

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 calculator’s 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

That’s all it takes. Now when you create a control of this type in another Visual Basic program, you’ll 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:


Figure 20.14   Setting a property to an ActiveX control.

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 we’ll 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.


TIP:  Another way to set up the Let and Get procedures for your properties in an ActiveX control is to use the ActiveX Control Interface Wizard in the Add-Ins menu (if that wizard does not appear in your Add-ins menu, select the Add-In Manager item in the Add-Ins menu and add the ActiveX Control Interface Wizard).

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


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.