Results 1 to 6 of 6

Thread: Iterating through contained controls. (Resolved)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Iterating through contained controls. (Resolved)

    Hi,

    When iterating through the controls on a form, it seems that controls contained within another control are ignored. Is that correct?

    Also, it seems impossible to iterate through a container control's controls.

    Any ideas please?
    Last edited by taxes; Jul 6th, 2004 at 09:16 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    You could use a recursive loop.

    VB Code:
    1. Public Sub LoopControls(Parent as Control)
    2.     Dim cc As ControlCollection
    3.     Dim c As Control
    4.  
    5.     If Parent is Nothing Then
    6.         cc=Me.Controls
    7.     Else
    8.         cc=Parent.Controls
    9.     End If
    10.  
    11.     For Each c In cc
    12.         'Do whatever it is you wanna do.
    13.  
    14.  
    15.         'Call the sub again, this time using the current control as parent.
    16.         If c.Controls.Count>0 Then LoopControls(c)
    17.     Next
    18. End Sub

    Just call it like LoopControls(Nothing) to start at the form itself.

    Hope this helps you.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Taxes,

    You are correct, controls contained in another control are not listed as part of the parent's controls, however it is possible to iterate through a container controls collection of contained controls.

    eg.
    VB Code:
    1. Dim ctl1 As Control, ctl2 As Control
    2. For Each ctl1 In Me.Controls
    3.     If ctl1.Name = "Panel1" Then
    4.         For Each ctl2 In ctl1.Controls
    5.             If ctl2.Name = "Button1" Then MessageBox.Show("Found It!")
    6.         Next
    7.     End If
    8. Next
    Last edited by CyberHawke; Jul 6th, 2004 at 07:09 AM.
    Whadayamean it doesn't work....
    It works fine on my machine!

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi CyberHawke

    Many thanks, it works O.K.

    Hi Pax,

    I'll try your suggestion later, Thanks.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Another option you could use when knowing which container control you are accessing is:

    VB Code:
    1. Dim ctl As Control
    2. For Each ctl In Me.Panel1.Controls
    3.     If ctl.Name = "Button1" Then MessageBox.Show("Found It!")
    4. Next
    Whadayamean it doesn't work....
    It works fine on my machine!

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by CyberHawke
    Another option you could use when knowing which container control you are accessing is:

    VB Code:
    1. Dim ctl As Control
    2. For Each ctl In Me.Panel1.Controls
    3.     If ctl.Name = "Button1" Then MessageBox.Show("Found It!")
    4. Next
    Even better
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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