Results 1 to 26 of 26

Thread: [RESOLVED] Detect what container a control is in

  1. #1

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Resolved [RESOLVED] Detect what container a control is in

    How can you step thru each control on a form and determine if it is in a container or not and what container it is in?

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Detect what container a control is in

    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

  3. #3

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Detect what container a control is in

    No that does not work... I tried that. The problem is that if a container is inside a container that test fails...

    This is what I have tried:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim ctl As Control
    5. Dim sStr As String
    6.  
    7.     On Error Resume Next
    8.    
    9.     For Each ctl In Controls
    10.    
    11.         Err.Clear
    12.         sStr = ctl.Container
    13.         If Err.Number = 0 And sStr <> "" Then
    14.             sStr = "In a Container - " & sStr & " - " & ctl.Parent.Name
    15.         Else
    16.             sStr = "A Container"
    17.         End If
    18.         Debug.Print ctl.Name & " - " & sStr
    19.        
    20.     Next
    21.    
    22. End Sub

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Detect what container a control is in

    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
    Example output:
    Frame4 Form1
    Picture2 Frame4
    Picture3 Picture2
    Picture1 Form1
    Frame3 Picture1
    Frame1 Form1
    Frame2 Frame1
    Label1 Frame1
    txtKey Form1
    MSComm1 Form1
    Command1 Form1

    The 'clue' is to use the Name property of the Container
    Last edited by Doogle; Mar 26th, 2008 at 12:29 AM.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    Another variation...
    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.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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Detect what container a control is in

    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)

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Detect what container a control is in

    The problem is that is a container is inside a container VB no longer considers it a container by these methods. This is my testing for this:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim ctl As Object
    5. Dim sStr As String
    6.  
    7.     On Error Resume Next
    8.    
    9.     For Each ctl In Controls
    10.    
    11.         Err.Clear
    12.         sStr = ctl.Container
    13.        
    14.         If Err.Number = 0 And sStr <> "" Then
    15.             sStr = "In a Container - " & sStr & " - " & ctl.Parent.Name
    16.             Debug.Print ctl.Name & " - " & sStr
    17.         Else
    18.             CheckContainer ctl
    19.            
    20.         End If
    21.        
    22.     Next
    23.    
    24. End Sub
    25.  
    26. Private Sub CheckContainer(Container As Object)
    27. Dim ctl As Object
    28. Dim sStr As String
    29. Dim i As Integer
    30.  
    31.  
    32.     On Error Resume Next
    33.    
    34.     Debug.Print vbCrLf & "Container - " & Container.Name
    35.    
    36.     For i = 0 To Container.Controls.Count - 1
    37.    
    38.         Err.Clear
    39.         sStr = Container.Controls(i).Name
    40.        
    41.         If Err.Number = 0 And sStr <> "" Then
    42.             CheckContainer ctl
    43.         Else
    44.             Debug.Print sStr
    45.         End If
    46.     Next
    47.    
    48. End Sub

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    but if you use .Parent and check if its not the form itself and check if its a .Container from the parent level it may tell you what you want to know.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Detect what container a control is in

    ctl.Parent.Container fails on everything.

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Detect what container a control is in

    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

    The results I get are :

    Form1->Frame1
    Form1->Frame1->Picture1
    Form1->Frame1->Picture1->Picture2
    Form1->Frame1->Picture1->Text2
    Form1->Text1

    What am I missing?
    Last edited by Doogle; Mar 26th, 2008 at 11:25 PM.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    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
    Attached Images Attached Images  
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Detect what container a control is in

    Doogle,

    You are missing nothing, it was me who was missing something. It was all in the technique use to find the answer.

    Thanks

  14. #14
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Detect what container a control is in

    Your code is not doing a test for container
    I think it is
    Code:
    On Error Resume Next
    If ctrl.Container.Name <> "" Then
        If Err.Number = 0 Then
    If the control doesn't have a container then Err.Number <> 0

    Anyway, glad it's sorted.

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Detect what container a control is in

    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")

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Detect what container a control is in

    RobDog888,

    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
    Attached Images Attached Images  

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    Ah close it was says I

    Seems the picturebox control is throwing it off. Hmm.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Detect what container a control is in

    While Doogle's code for the same window shows:
    Code:
    Form1->Frame1
    Form1->Frame1->Picture1
    Form1->Frame1->Picture1->Command1
    Form1->Frame1->Text1
    Form1->Frame2
    Form1->Frame2->Frame3
    Form1->Frame2->Frame3->Picture2
    Form1->Frame2->Frame3->Picture2->Drive1

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    Hmm, it worked for me.

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  22. #22
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  23. #23

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Detect what container a control is in

    Ok, now that worked somewhat, it just doesn't tell me the actual name of the container.

  24. #24

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Detect what container a control is in

    But if I do this it does...

    vb Code:
    1. Private Sub Command1_Click()
    2.     Dim ctrl As Control
    3.     For Each ctrl In Me.Controls
    4.         If TypeName(ctrl.Container) <> Me.Name Then
    5.             Debug.Print "Control: " & ctrl.Name & " has a container of - " & ctrl.Container.Name
    6.         Else
    7.             Debug.Print "Control: " & ctrl.Name & " has a container of - " & ctrl.Container.Name
    8.         End If
    9.     Next
    10. 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

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detect what container a control is in

    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.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width