|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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
Thats all we need; now create the calculator control and embed it in another program as, say, CalculatorControl1. When you do, you can use the controls Calculate method like this: CalculatorControl1.Calculate. For example, weve added a new button with the caption Calculate to the project in Figure 20.16, and added this code to the command buttons Click event:
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 controls Calculate method. Our ActiveX method example is a success. The code for this example, activexcalculatorcontrol.ctl version 3, appears in Listing 20.3.
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 ControlActiveX 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 methodswith the Add Procedure item in the Tools menu. After you create a new event, its up to you to raise that event with the RaiseEvent method. Lets see an example. Here, well add an event named CalculatorClick to the calculator ActiveX control weve 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.
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 controls (General) section: Public Event CalculatorClick() If you want to add arguments to this events handler procedures, just list them in the parentheses as you would for any procedure. How do we make this new event active? Its up to us to raise this event when appropriate, using the RaiseEvent method. In this case, thats particularly easywell just use the user controls 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 well raise the CalculatorClick event here:
Private Sub UserControl_Click()
RaiseEvent CalculatorClick
End Sub
Thats all we need. Now when you embed the calculator control in a Visual Basic program, youll find that it has a CalculatorClick event that you can add text to:
Private Sub CalculatorControl1_CalculatorClick()
CalculatorControl1.Calculate
End Sub
|
|
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. |