How To Detect When a Control Is Placed into a UserControl (174553)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q174553

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.

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto KB174553