Ian Post
7/6/2011 3:58:00 PM
BeeJ laid this down on his screen :
> Eduardo formulated the question :
>> El 05/07/2011 10:19 p.m., BeeJ escribió:
>>> I want to create a control / parent / container info.
>>
>> Public Function GetContainedControls(nObject As Object, _
>> Optional ByVal nIndentation As Long = 0) As String
>>
>> Dim iParent As Form
>> Dim iStr As String
>> Dim iCtl As Control
>>
>> If TypeOf nObject Is Form Then
>> Set iParent = nObject
>> Else
>> On Error Resume Next
>> Set iParent = nObject.Parent
>> On Error GoTo 0
>> End If
>> If iParent Is Nothing Then Exit Function
>>
>> iStr = String$(nIndentation, " ") & nObject.Name & vbCrLf
>> nIndentation = nIndentation + 4
>>
>> On Error Resume Next
>> For Each iCtl In iParent
>> Err.Clear
>> If iCtl.Container Is nObject Then
>> If Err.Number = 0 Then
>> iStr = iStr & GetContainedControls(iCtl, nIndentation)
>> End If
>> End If
>> Next
>>
>> GetContainedControls = iStr
>> End Function
>
> Perfect!
> Thank you!
> I am able to follow it too.
> I knew recursion was the answer but could not figure it out.
I added this routine to get the hierarchy for a control.
The result of your routine, with my mods, was placed in g_sCtrlTree
Does this look OK?
usage
Debug.Print "Control Family " & ControlHierarchy("optUSBSel(0)")
Public Function ControlHierarchy(sCtrlName As String) As String
On Error GoTo ControlHierarchyErr
Dim asLine() As String
Dim lX As Long
Dim sFamily As String
Dim lIndent As Long
asLine = Split(g_sCtrlTree, vbCrLf)
For lX = 0 To UBound(asLine)
If InStr(1, asLine(lX), sCtrlName) > 0 Then
Exit For
End If
Next lX
If lX <= UBound(asLine) Then
' back up
lIndent = Len(asLine(lX)) - Len(LTrim$(asLine(lX)))
sFamily = asLine(lX)
Do
lIndent = lIndent - clIndent
Do
lX = lX - 1
Loop Until (Len(asLine(lX)) - Len(LTrim$(asLine(lX)))) =
lIndent
sFamily = asLine(lX) & sFamily
Loop Until lIndent = 0
End If
ControlHierarchy = sFamily
ControlHierarchyExit:
Exit Function
ControlHierarchyErr:
Debug.Assert False
Resume ControlHierarchyExit
End Function 'ControlHierarchy