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


Following these steps creates a new procedure, Calculate, in the calculator ActiveX control:

Public Sub Calculate()

End Sub

If you have arguments you want passed to this method, just add them to the argument list between the parentheses as you would for any subroutine. In this case, we want to calculate the sum of the two operands now in Text1 and Text2 in this method, storing the sum in Text3, and we do that with this code:

Public Sub Calculate()
     Text3.Text = Str(Val(Text1.Text) + Val(Text2.Text))
End Sub

That’s all we need; now create the calculator control and embed it in another program as, say, CalculatorControl1. When you do, you can use the control’s Calculate method like this: CalculatorControl1.Calculate.

For example, we’ve added a new button with the caption Calculate to the project in Figure 20.16, and added this code to the command button’s Click event:


Figure 20.16  Calling a custom ActiveX control method.

Private Sub Command1_Click()
     CalculatorControl1.Calculate
End Sub

The result appears in Figure 20.16. When the user clicks the Calculate button, we execute the calculator ActiveX control’s Calculate method. Our ActiveX method example is a success. The code for this example, activexcalculatorcontrol.ctl version 3, appears in Listing 20.3.


TIP:  Another way to set up methods 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.3 activexcalculatorcontrol.ctl version 3

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
    PropertyChanged "Operand1"
End Property

Public Property Get Operand2() As Variant
    Operand2 = Text2.Text
End Property

Public Property Let Operand2(ByVal vNewValue As Variant)
    Text2.Text = vNewValue
    PropertyChanged "Operand2"
End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    Text1.Text = PropBag.ReadProperty("Operand1")
    Text2.Text = PropBag.ReadProperty("Operand2")
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "Operand1", Text1.Text
    PropBag.WriteProperty "Operand2", Text2.Text
End Sub

Public Sub Calculate()
    Text3.Text = Str(Val(Text1.Text) + Val(Text2.Text))
End Sub

Adding An Event To An ActiveX Control

ActiveX controls can support events, of course, and the custom ActiveX controls you design with Visual Basic are no exception. You add events much like you add properties and methods—with the Add Procedure item in the Tools menu. After you create a new event, it’s up to you to raise that event with the RaiseEvent method.

Let’s see an example. Here, we’ll add an event named CalculatorClick to the calculator ActiveX control we’ve developed in the previous few topics. When the user clicks the calculator control, this event, CalculatorClick, will occur.

To add this event to the calculator ActiveX control, open the calculator ActiveX project in Visual Basic, and open the code window as well. Next, select the Add Procedure item in the Tools menu to open the Add Procedure dialog box, as shown in Figure 20.17.


Figure 20.17  Adding an event to an ActiveX control.

Type the name of this event, “CalculatorClick”, into the Name box in the Add Procedure dialog box, select the option button labeled Event to indicate that we want to create a new event, and click OK. This creates the new event by declaring it in the ActiveX control’s (General) section:

Public Event CalculatorClick()

If you want to add arguments to this event’s handler procedures, just list them in the parentheses as you would for any procedure.

How do we make this new event active? It’s up to us to raise this event when appropriate, using the RaiseEvent method. In this case, that’s particularly easy—we’ll just use the user control’s Click event. Add a Click event handler to the calculator ActiveX control now:

Private Sub UserControl_Click ()

End Sub

This is the event handler that will be called when the calculator control is clicked, and we’ll raise the CalculatorClick event here:

Private Sub UserControl_Click()
     RaiseEvent CalculatorClick
End Sub


TIP:  If your event supports arguments, you raise it and pass the arguments to it like this
RaiseEvent eventname([argumentlist])

just as you would pass arguments to any procedure.


That’s all we need. Now when you embed the calculator control in a Visual Basic program, you’ll find that it has a CalculatorClick event that you can add text to:

Private Sub CalculatorControl1_CalculatorClick()
    CalculatorControl1.Calculate
End Sub


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.