HOWTO: Detect When a Control Is Placed into a UserControl

Last reviewed: October 2, 1997
Article ID: Q174553
The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0

SUMMARY

This article illustrates how you can create an ActiveX control that detects when a control is placed on it at design time.

MORE INFORMATION

Step-by-Step Example

The following example creates a custom ActiveX control that can perform some action when another control is placed on top of it:

  1. Start a new ActiveX Control project. UserControl1 is added by default.

  2. Set the ControlContainer property to True.

  3. Add a Timer control to UserControl1. Add the following code to the Timer event of Timer1:

          Private Sub Timer1_Timer()
    
             Dim UserCtrl As Object
             Debug.Print UserControl.ContainedControls.Count
             For Each UserCtrl In UserControl.ContainedControls
                Debug.Print UserCtrl.Name
             Next
    
          End Sub
    
    

  4. Set the Timer1 Interval property to 1000 so Private Sub Timer1_Timer() gets called every 1 second.

  5. Close the UserControl1 design window.

  6. Click Add Project on the File menu. Select "Standard EXE" and click Open.

    Note: You now have a project group consisting of an ActiveX control project and a Standard EXE project.

  7. Click Immediate Window on the View menu.

  8. Draw the UserControl1 (the control you just created in your group) on Form1.

  9. Select any other control from the toolbox and put it on top of UserControl1.

RESULTS: When you draw another control on top of UserControl1, the count of how many controls you have added plus the name of each control appears in the Immediate Window.


Additional query words: Active
Keywords : VB4ALL VBKBAX_VBKBComp
Version : WINDOWS:5.0
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: October 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.