Microsoft KB Archive/241434

= How To Access Nested ActiveX Control's Property/Method by Name from Web Page Script =

Article ID: 241434

Article Last Modified on 5/11/2006

-

APPLIES TO


 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.01 Service Pack 1
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q241434



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:


 * 1) 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.
 * 2) 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.</li> Select Components from the Project menu, and add ActiveX Control InnerCtrl.ocx.</li> Place an instance of the InnerCtrl on the ucOuterCtrl. By default it is named ucInnerCtrl1.</li>  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

</li> Compile the project.</li></ol>

Create a Test HTML Page
 Create an Internet Package for the OuterCtrl using Package and Deployment Wizard (PDW).</li>  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>

<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> </li> Run the HTM page and test the application.</li></ol>

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.

<div class="references_section">