Results 1 to 5 of 5

Thread: [RESOLVED] Recursively looping through UserControls?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Recursively looping through UserControls?

    I have a tabPage that contains a dynamically created UserControl (UC1). This UserControl contains several smaller UserControls (ucSubs?). What I need to be able to do is loop through each ucSub? I have no idea how to set this up!

    Thanks,
    Blake

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Recursively looping through UserControls?

    Would something like this work?

    This passes the form into the recursive method and will display a message box with the name any control and subcontrols on the form.

    VB Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.         ListControls(Me)
    3.     End Sub
    4.  
    5.     Private Sub ListControls(control As Control)
    6.         For Each subControl As Control In control.Controls
    7.             If (subControl.Controls.Count > 0) Then
    8.                 ListControls(subControl)
    9.             End If
    10.  
    11.             MessageBox.Show(subControl.Name)
    12.         Next
    13.     End Sub

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Recursively looping through UserControls?

    Your suggestion almost works.

    Here is the hierarchy:

    Main UC
    |
    ---> SubUC
    |
    ---> controls

    I need to be able to loop thru the Main UC and then loop thru each SubUC in order to examine the controls.
    Blake

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Recursively looping through UserControls?

    How bout,
    Code:
    For Each sub_UC In Uc1.Controls.OfType(Of subUC)()
        Debug.WriteLine(sub_UC.Name)
    
        For Each cntrl As Control In sub_UC.Controls
            Debug.WriteLine(cntrl.Name)
        Next
    
    Next

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Recursively looping through UserControls?

    that worked!

    thanks Edgemeal
    Blake

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