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



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 4.0
  • Microsoft Internet Explorer (Programming) 4.01
  • Microsoft Internet Explorer (Programming) 4.01 SP1
  • Microsoft Internet Explorer (Programming) 4.01 SP2
  • Microsoft Internet Explorer (Programming) 5
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

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

  1. Start Visual Basic and create a new ActiveX Control project.
  2. Name the project as InnerCtrl. And the control as ucInnerCtrl.
  3. 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
    
    					
  4. Compile and save the project.

Create a Test Outer Control

  1. Create another ActiveX Control project.
  2. Name the project OuterCtrl and the control as ucOuterCtrl.
  3. Select Components from the Project menu, and add ActiveX Control InnerCtrl.ocx.
  4. Place an instance of the InnerCtrl on the ucOuterCtrl. By default it is named ucInnerCtrl1.
  5. 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
    
    					
  6. Compile the project.


Create a Test HTML Page

  1. Create an Internet Package for the OuterCtrl using Package and Deployment Wizard (PDW).
  2. 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>
    					
  3. 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.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

186143 How To Use the CallByName Function to Run a Procedure

For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:5/11/2006
Keywords:kbhowto KB241434