Something like this perhaps. It makes the assumption that if the Control doesn't have a Container Property then it is contained by the Form. (Not sure if that's a valid assumption or not)
Code:
Dim ctl As Control
For Each ctl In Controls
Debug.Print ctl.Name; " ";
On Error Resume Next
Debug.Print ctl.Container
If Err.Number <> 0 Then
Debug.Print Me.Name
End If
Next
This works for me when I have Frames within Frames, Frames within PictureBoxes
(Edit: and PictureBoxes in Frames and PictureBoxes in PictureBoxes))
Code:
Dim ctl As Control
For Each ctl In Controls
Debug.Print ctl.Name; " ";
On Error Resume Next
Debug.Print ctl.Container.Name
If Err.Number <> 0 Then
Debug.Print Me.Name
End If
Next
Option Explicit
Private Sub Command1_Click()
Dim ctrl As Control
For Each ctrl In Me.Controls
Debug.Print "Control: " & ctrl.Name & " has a container of - " & TypeName(ctrl.Parent)
Next
End Sub
'Control: Frame1 has a container of - Form1
'Control: Check1 has a container of - Form1
'Control: Text1 has a container of - Form1
'Control: Command1 has a container of - Form1
'Control: Label1 has a container of - Form1
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
RobDog888: I get strange results with your variation
Code:
Control: Command1 has a container of - Form1
Control: Picture1 has a container of - Form1
Control: Picture2 has a container of - Form1
Control: Picture3 has a container of - Form1
Control: Frame1 has a container of - Form1
Control: Text1 has a container of - Form1
Control: Timer1 has a container of - Form1
Control: cmdDown has a container of - Form1
Control: cmdUp has a container of - Form1
Control: List1 has a container of - Form1
But with my version
Code:
Private Sub Form_Load()
Dim ctl As Control
For Each ctl In Controls
Debug.Print ctl.Name; " is contained in ";
On Error Resume Next
Debug.Print ctl.Container.Name
If Err.Number <> 0 Then
Debug.Print Me.Name
End If
Next
End Sub
I get:
Code:
Command1 is contained in Form1
Picture1 is contained in Form1
Picture2 is contained in Picture1
Picture3 is contained in Picture2
Frame1 is contained in Picture3
Text1 is contained in Form1
Timer1 is contained in Form1
cmdDown is contained in Form1
cmdUp is contained in Form1
List1 is contained in Form1
There's a subtle difference between Container and Parent.
(BTW the results from my code refect the real situation)
A parent and a container are similar. It depends on what randem is needing. Also what controls and how they are set on a form.
If a control is on a form then its parent is the form but it is also contained in the form too. If he only wants controls that can host other controls then dont use my code. I have no idea and am not psychic as to what and how your controls are on your form. As I said its a variation to give him options, not an improvement f the code you posted.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
I must be missing the point.
I don't understand this statement: "The problem is that is (sic) a container is inside a container VB no longer considers it a container by these methods"
Code:
Private Sub Form_Load()
Dim ctrl As Control
For Each ctrl In Controls
Call FindContainer(ctrl)
Debug.Print
Next ctrl
End Sub
Private Sub FindContainer(ctrl As Object)
Dim objCtrl As Object
On Error Resume Next
If ctrl.Container.Name <> "" Then
If Err.Number = 0 Then
On Error GoTo 0
Set objCtrl = ctrl.Container
Call FindContainer(objCtrl)
Debug.Print "->"; ctrl.Name;
Else
On Error GoTo 0
Debug.Print ctrl.Name;
End If
End If
End Sub
Will show each control's position within the 'hierarchy' of containers
eg
I have a Form with a TextBox, and a Frame. Within the Frame I have a PictureBox, within that PictureBox I have a Textbox and another PictureBox
Hes saying that he can not detect a nested container. Your code is not doing a test for container but just looping through the controls on a form.
I think this is more of what he is asking for. Simple code. Since no containers at the form level can have a container (as the form itself is a type of container) the second level of nested containers will come up correct all the way down.
Code:
Option Explicit
Private Sub Command1_Click()
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeName(ctrl.Container) <> Me.Name Then
Debug.Print "Control: " & ctrl.Name & " has a container of - " & ctrl.Container
Else
Debug.Print "Control: " & ctrl.Name & " has a container of - " & TypeName(ctrl.Container)
End If
Next
End Sub
'Control: Text1 has a container of - Form1
'Control: Frame1 has a container of - Form1
'Control: Frame2 has a container of - Frame1
'Control: Command1 has a container of - Frame2
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
On Error Resume Next is never a good way to handle errors.
When you get into larger projects using that technique it can lead to many unforseen issues which increases your debugging time and throw your budgeted timeline way out of wack.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
That's why I used On Error GoTo 0 as soon as I had examined the Err Object.
In this case the only error that could be generated is "438 Object does not support this property or method" which means that there is no container. I think that, when used correctly, On Error Resume Next is handy to perform in-line error checking. (But I emphasise "when used correctly")
No it doesnt mean that as I explained the control could reside on the Form which is not a Container but rather a host which is similar to a container. the Form contains controls but its from a different logic/design.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
You method has some flaws for this type of window:
Code:
Control: Frame2 has a container of - Form1
Control: Frame3 has a container of - Frame2
Control: Picture2 has a container of - Frame3
Control: Drive1 has a container of - 0
Control: Frame1 has a container of - Form1
Control: Text1 has a container of - Frame1
Control: Picture1 has a container of - Frame1
Control: Command1 has a container of - 0
Picturebox at form level and a picturebox nested inside frame 2.
Code:
Control: Picture1 has a container of - Form1
Control: Text1 has a container of - Form1
Control: Frame1 has a container of - Form1
Control: Frame2 has a container of - Frame1
Control: Picture2 has a container of - Frame2
Control: Command1 has a container of - Frame2
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
I duplicated your object heirarchy and fixed it simply with a typename call.
Code:
Option Explicit
Private Sub Command1_Click()
Dim ctrl As Control
For Each ctrl In Me.Controls
Debug.Print "Control: " & ctrl.Name & " has a container of - " & TypeName(ctrl.Container)
Next
End Sub
Control: Frame2 has a container of - Form1
Control: Frame3 has a container of - Frame2
Control: Picture1 has a container of - Frame3
Control: Drive1 has a container of - PictureBox
Control: Frame1 has a container of - Form1
Control: Text1 has a container of - Frame
Control: Picture2 has a container of - Frame
Control: Command1 has a container of - PictureBox
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Debug.Print "Control: " & ctrl.Name & " has a container of - " & ctrl.Container.Name
Else
Debug.Print "Control: " & ctrl.Name & " has a container of - " & ctrl.Container.Name
End If
Next
End Sub
Code:
Control: Frame2 has a container of - Form1
Control: Frame3 has a container of - Frame2
Control: Picture2 has a container of - Frame3
Control: Drive1 has a container of - Picture2
Control: Frame1 has a container of - Form1
Control: Text1 has a container of - Frame1
Control: Picture1 has a container of - Frame1
Control: Command1 has a container of - Picture1
Well its closer then before and alot shorter The typename is getting the type of the object to prevent the "0" Container display. Multiple ways to skin a cat. You can choose or mix/match as you need.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
I had edited my code previously. So here is the addition of your part with my edit. Massivly complex code!!!
Code:
Option Explicit
Private Sub Command1_Click()
Dim ctrl As Control
For Each ctrl In Me.Controls
Debug.Print "Control: " & ctrl.Name & " has a container of - " & ctrl.Container.Name
Next
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.