HOWTO: Access Nested ActiveX Control's Property/Method by Name from Web Page Script
ID: Q241434
|
The information in this article applies to:
-
Microsoft Internet Explorer (Programming) versions 4.0, 4.01, 4.01 SP1, 4.01 SP2, 5
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0
SUMMARY
When hosting an ActiveX Control on a HTML page, there are occasions wherein you need to call methods or properties of any nested controls in the ActiveX Control. This article demonstrates how to do that.
This is achieved by having public methods in the parent ActiveX Control and by using the CallByName function to call methods and properties of the inner control.
MORE INFORMATION
Consider an ActiveX control called OuterCtrl that embeds another ActiveX control called InnerCtrl. In a Web page where the OuterCtrl is sited, we can access the InnerCtrl's properties or methods by name using script.
To achieve this, do the doing following:
- In the Visual Basic code for OuterCtrl, create two functions GetProp and CallMethod. GetProp is used to access the embedded control's property, and CallMethod is to call the method.
- In the Web page script, call one of the above functions of OuterCtrl and pass the appropriate method or property name of the InnerCtrl, along with required parameters.
The following sample projects demonstrate the above idea.
Create a Test Inner Control
- Start Visual Basic and create a new ActiveX Control project.
- Name the project as InnerCtrl. And the control as ucInnerCtrl.
- Add the following code to ucInnerCtrl:
Option Explicit
Const m_def_MyValue = 1
'Property Variables:
Dim m_MyValue As Variant
Public Property Get MyValue() As Variant
MyValue = m_MyValue
End Property
Public Property Let MyValue(ByVal New_MyValue As Variant)
m_MyValue = New_MyValue
PropertyChanged "MyValue"
End Property
Private Sub UserControl_InitProperties()
m_MyValue = m_def_MyValue
End Sub
'Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
m_MyValue = PropBag.ReadProperty("MyValue", m_def_MyValue)
End Sub
'Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("MyValue", m_MyValue, m_def_MyValue)
End Sub
Public Sub MultiplyBy(ByVal intValue As Integer)
MsgBox "The result is :" & MyValue * intValue
End Sub
- Compile and save the project.
Create a Test Outer Control
- Create another ActiveX Control project.
- Name the project OuterCtrl and the control as ucOuterCtrl.
- Select Components from the Project menu, and add ActiveX Control InnerCtrl.ocx.
- Place an instance of the InnerCtrl on the ucOuterCtrl. By default it is named ucInnerCtrl1.
- Add the following code to ucOuterCtrl:
Option Explicit
Public Function GetProp(ByVal strPropertyName As String) As Variant
GetProp = CallByName(ucInnerCtrl1, strPropertyName, VbGet)
End Function
Public Function LetProp(ByVal strPropertyName As String, vSomeParameter As Variant) As Variant
LetProp = CallByName(ucInnerCtrl1, strPropertyName, VbLet, vSomeParameter)
End Function
Public Sub CallMethod(ByVal strMethodName As String, vSomeParameter As Variant)
CallByName ucInnerCtrl1, strMethodName, VbMethod, vSomeParameter
End Sub
- Compile the project.
Create a Test HTML Page
- Create an Internet Package for the OuterCtrl using Package and Deployment Wizard (PDW).
- Modify the OuterCtrl.HTM file generated by PDW as follows. Please do retain the entire OBJECT tag generated on your machine.
<HTML>
<HEAD>
<TITLE>Sample HTML page for KB article Q241434</TITLE>
<SCRIPT LANGUAGE="VBScript">
Sub btnGetProperty_OnClick()
Msgbox "MyValue = " & ucOuterCtrl.GetProp("MyValue")
End Sub
Sub btnLetProperty_OnClick()
ucOuterCtrl.LetProp "MyValue", txtMyValue.value
Msgbox "MyValue has changed"
End Sub
Sub btnCallMethod_OnClick()
ucOuterCtrl.CallMethod "MultiplyBy", 5
End Sub
</SCRIPT>
</HEAD>
<BODY>
<!-- Please use your own class ID and version number generated by the PDW-->
<OBJECT ID="ucOuterCtrl"
CLASSID="CLSID:9F5AAB10-CC19-4DD5-B3C7-B3C6F3A15C5F"
CODEBASE="OuterCtrl.CAB#version=1,0,0,0">
</OBJECT>
<P> Click the button "GetProp" to retrieve the MyValue property of the inner control.
<INPUT TYPE=button ID=btnGetProperty VALUE="GetProp">
<P> Click the button "LetProp" to set the MyValue property of the inner control.
<INPUT TYPE=button ID=btnLetProperty VALUE="LetProp">
MyValue = <INPUT TYPE=text VALUE="5" NAME="txtMyValue" ID="txtMyValue" >
<P> Then click <B>CallMethod</B> to see the result of MyValue multiply by 5.
<INPUT TYPE=button ID=btnCallMethod VALUE="CallMethod">
</BODY>
</HTML>
- Run the HTM page and test the application.
For the above code, please note that the .htm and .cab files need to be located on same folder. Also depending on your security settings, you might get a security warning.
REFERENCESFor additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
Q186143 HOWTO: Use the CallByName Function to Run a Procedure
For more information, please see the MSDN Web Workshop:
http://msdn.microsoft.com/workshop/default.asp
Additional query words:
CallByName ActiveX Control Nested Script
Keywords : kbActiveX kbJScript kbVBScript
Version : WINDOWS:4.0,4.01,4.01 SP1,4.01 SP2,5,6.0
Platform : WINDOWS
Issue type : kbhowto
|