HOWTO: Iterate Controls on a Form from a Property Page

Last reviewed: March 18, 1997
Article ID: Q161056
The information in this article applies to:
  • Professional and Enterprise Editions of Microsoft Visual Basic, for Windows, version 5.0
  • Microsoft Visual J++, version 1.0

SUMMARY

Microsoft Visual Basic version 5.0 enables you to create custom property pages for user-defined ActiveX controls. These property pages can contain controls and fields that change the properties of the user-defined control.

The property pages can also be extended to change properties of other controls that may be contained within the form where the user-defined control is located.

The property page, by default, can only access public properties of the user-defined control for which it was written. However, by using the ParentControls collection and a Friend function, you can create a route through which you can expose the other controls contained within the parent form.

The use of the Friend function enables components within the ActiveX control to use this function, while hiding it from use outside the immediate project. The ParentControls collection returns the objects within the parent container. This includes returning item(0) as a reference to the parent container.

MORE INFORMATION

To enable a property page for a user-defined ActiveX control to gain access to the container form's set of controls, do the following:

  1. Start Microsoft Visual Basic. Load an ActiveX control project, or create a new one.

  2. Add the following code in the control's code window:

          Friend Function FormControls() As Object
    
              Set FormControls = ParentControls
              'Returns the collection of controls on the parent form.
          End Function
    
    

  3. From the Project menu, click Add Property Page.

  4. Add the following code in the Property Page's code window:

          Private Sub PropertyPage_SelectionChanged()
    
              Dim UC As UserControl1  'Make sure that UC is typed explicitly as
                                      'the user-defined control you are working
                                      'with. To use Friend functions, you must
                                      'early-bind your variables.
              Dim Ctrl As Object
              Set UC = SelectedControls(0)  'Return selected control.
              For Each Ctrl In UC.FormControls  'Iterate through the use of
                                                'UC's friend function.
              Debug.Print Ctrl.Name  'Output the name of each control.
              Next Ctrl
          End Sub
    
    

  5. Switch to the UserControl1 object. On the properties list, click Property Pages. Turn on the option for PropertyPage1.

Now when you use this custom ActiveX control on a form, you can choose
'Custom' from the properties list to bring up the Property Page.

REFERENCES

Microsoft Visual Basic Online Help Keywords: Friend, Extender


Additional query words: hidden private
Keywords : kbusage vb5all vb5howto VBKBComp VBKBCtrl_VBKBAX_VB kbhowto
Version : 5.00
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.