Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

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 controls...one 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
Screenshot.JPG
Avatar of kaufmed
kaufmed
Flag of United States of America image

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.
Avatar of BlakeMcKenna

ASKER

Sorry...here you go! The below code resides in a Form's code-behind.

    Private Sub AddChannelsToPanel(ByVal strChannel As String)

        Dim blnExitSub As Boolean = False

        Try
            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
                Next

                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)
            pnlChannels.Controls.Add(rdo)

            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
            uC.BringToFront()

            iWidth += uC.Width + 5

            Panel1.Controls.Add(uC)

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

Open in new window

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?
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" ...
            ucs.foo = 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.
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?
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)?
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!
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks CodeCruiser...that helped!
Interesting...  I thought both of CodeCruiser's suggestions were mentioned earlier  : \