'以下示例列出所有Picture1内的控件 Private Function IsInside(ByVal SubControl As Control, ByVal Container As Control) As Boolean Dim ctl As Control Set ctl = SubControl While Not ctl.Container Is Me If ctl.Container Is Container Then IsInside = True Exit Function End If Set ctl = ctl.Container Wend End FunctionPrivate Sub Command1_Click() Dim ctl As Control For Each ctl In Me.Controls If IsInside(ctl, Picture1) Then Debug.Print ctl.Name End If Next End Sub
Private Function IsInside(ByVal SubControl As Control, ByVal Container As Control) As Boolean
Dim ctl As Control
Set ctl = SubControl
While Not ctl.Container Is Me
If ctl.Container Is Container Then
IsInside = True
Exit Function
End If
Set ctl = ctl.Container
Wend
End FunctionPrivate Sub Command1_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If IsInside(ctl, Picture1) Then
Debug.Print ctl.Name
End If
Next
End Sub
IsInside()中有个上溯的过程,你可以做个计数以求得控件所在的层数,自己按层数进行排序。