Results 1 to 3 of 3

Thread: [RESOLVED] How to make a Collection from a list of UserControlls to call in For-Each?

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [RESOLVED] How to make a Collection from a list of UserControlls to call in For-Each?

    If their number is specific we would do this
    Code:
    Dim UC As UserControl1() = {UserControl11, UserControl12, UserControl13, UserControl14}
            For i As Integer = 1 To 4
                UC(i - 1).Enabled = True
            Next
    And do some stuffs on all of them at once.

    Now let's suppose UserControl1 would be added programmatically in a various uncertain number each time. Plus there is "For Each" sort of loop which could be make it easier. How can we use it? A proper way. How can we perform a similar thing here?

    Note:
    They're added in TableLayoutPanel if it helps.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to make a Collection from a list of UserControlls to call in For-Each?

    vb.net Code:
    1. Dim userControls = myTableLayoutPanel.Controls.OfType(Of UserControl1)().ToArray()
    If you're going to use a For Each loop then you don;t even need the array. You can enumerate the result of OfType directly:
    vb.net Code:
    1. For Each uc1 In myTableLayoutPanel.Controls.OfType(Of UserControl1)()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: How to make a Collection from a list of UserControlls to call in For-Each?

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. For Each uc1 In myTableLayoutPanel.Controls.OfType(Of UserControl1)()
    Fantastic! That's what I was looking for... Thanks.

Tags for this Thread

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