Creating dynamic controls that can be references throughout a Form module?

I have a Windows Form that has a subprocedure in which it creates two different instances of being a custom UC that contains multiple comboboxes/textboxes. When I create an instance of these controls, I assign them a unique name, however, they are created within a subprocedure so my thinking is that they cannot be globally accessed from within the Form. How can I be able to access each instance of these controls? I have attached a screenshot as well as the code
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
There's no code attached, but to me it sounds like you simply need to widen the scope of the reference. As long as the dynamic controls are in the appropriate scope, then that scope should be able to reference the controls.
BlakeMcKennaAuthor Commented: you go! The below code resides in a Form's code-behind.

    Private Sub AddChannelsToPanel(ByVal strChannel As String)

        Dim blnExitSub As Boolean = False

            EH.ErrorMessage = String.Empty

            If arrChannels.Length > 0 Then
                For x As Integer = 0 To arrChannels.Length - 1
                    If arrChannels(x) = strChannel Then
                        blnExitSub = True
                        Exit For
                    End If

                If blnExitSub Then
                    Exit Sub
                End If
            End If

            'This section will add a new instance of a radiobutton for the selected Channel
            Dim rdo As New RadioButton

            rdo.Name = "rdo_" & iCnt
            rdo.Text = strChannel
            rdo.Left = 1
            rdo.Tag = iCnt
            rdo.Top = iHeight

            AddHandler rdo.Click, AddressOf rdo_Click

            iHeight += (rdo.Height - 5)

            ReDim Preserve arrChannels(iCnt)
            arrChannels(iCnt) = strChannel
            iCnt += 1

            'This section will add a new instance of the userChannelSettings UserControl
            Dim uC As New userChannelSettings

            uC.Name = "uC_" & strChannel
            uC.mlChannelName.Text = "Channel:  " & strChannel
            uC.Left = iWidth
            uC.Top = 1
            uC.Visible = True

            iWidth += uC.Width + 5


        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/AddChannelsToPanel() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window

käµfm³d 👽Commented:
OK. As I mentioned, you would need to widen the scope. This could be as simple as moving the declaration of the variable to be class level instead of method level; it could be as complicated as creating a collection to hold the new controls as they are created. Are you only creating one (type of) control per invocation of the AddChannelsToPanel method?
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
While widening the scope is certainly one way to tackle the problem, you could simply use that unique name to search for the control:
        Dim ctrlName = "uC_" & something
        Dim matches() As Control = Me.Controls.Find(ctrlName, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is userChannelSettings Then
            Dim ucs As userChannelSettings = DirectCast(matches(0), userChannelSettings)
            ' ... do something with "ucs" ...
   = bar
        End If

Open in new window

Given that you are adding these controls to Panel1, which I am assuming is accessible everywhere, you can just loop through Panel1's controls collection.
BlakeMcKennaAuthor Commented:
Each control I created, has it's own name assigned to it. That is what I need to reference in other portions of the Form's logic. How do I do that?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You need the NAME of the controls available to other portions of your program?...or you want to get a REFERENCE to the CONTROLS (based on a given unique name)?
BlakeMcKennaAuthor Commented:
I need to be able to reference these controls from within the module their created in. I think what CodeCruiser suggested might work. The Form module is called "frmCalibration". The sub that creates these controls resides within "frmCalibration". Those controls just to need to be accessible from within "frmCalibration". Hope that helps!
There are two issues

1) You are creating controls within a method so these are not referenced outside the method
2) You are reusing an existing variable to create a new instance which means your reference gets overwritten

You have two options

1) Access these controls using the Panel's controls collection
2) If the controls collection has other controls as well then you can create a form level list of these controls and keep adding these controls to the list whenever you create one.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
Thanks CodeCruiser...that helped!
käµfm³d 👽Commented:
Interesting...  I thought both of CodeCruiser's suggestions were mentioned earlier  : \
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.