Accessing controls from within a dynamically created Form?

I have an Event within a Form  module that creates an instance of an existing Form. I added an AddHandler statement because this process is done within a For Loop so multiple instances can be created. What I'm wondering is...how do I access the controls within one of the instances of the Form? I have included the Event as well as the AddHandler Event.

    Private Sub TextBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox4.KeyPress, TextBox5.KeyPress, TextBox9.KeyPress, TextBox15.KeyPress, TextBox18.KeyPress
        Try
            EH.ErrorMessage = String.Empty

            If e.KeyChar = ChrW(Keys.Return) Then
                Dim iHeight As Integer = 0
                Dim pnl5 As New Panel

                GroupBox13.Controls.Add(pnl5)
                GroupBox13.Visible = True

                Dim cbo As CheckBox

                'Option 5 ========================================================================
                For x As Integer = 0 To 5
                    cbo = New CheckBox
                    pnl5.Dock = DockStyle.Fill
                    pnl5.Controls.Add(cbo)

                    Select Case x
                        Case 3
                            cbo.Text = "Fx-1"
                        Case 4
                            cbo.Text = "Fy-1"
                        Case 5
                            cbo.Text = "Fz-1"
                        Case Else
                            cbo.Text = x + 1
                    End Select

                    cbo.Checked = True
                    cbo.Name = x + 1
                    cbo.Tag = cbo.Text
                    cbo.Location = New Point(5, iHeight)

                    AddHandler cbo.Click, AddressOf cbo_Click

                    GroupBox13.Height = iHeight + 30
                    iHeight += cbo.Height - 4

                    Dim uC5 As New userChannelSettings

                    uC5.Name = cbo.Text
                    uC5.Tag = cbo.Text
                    uC5.mlChannelName.Text = "Channel:  " & cbo.Text
                    uC5.Left = iWidth
                    uC5.Top = 1
                    uC5.Visible = True
                    uC5.BringToFront()

                    iWidth += uC5.Width + 5

                    Panel2.Controls.Add(uC5)


                    Dim frm As New frmLoadTest		===================> Instantiating a new instance of existing Form
                    Dim tab As New TabPage

                    frm.TopLevel = False

                    AddHandler frm.Load, AddressOf frm_Load

                    tabOption5Load.TabPages.Add(tab)
                    tab.Text = "Channel " & cbo.Text

                    tab.Controls.Add(frm)
                    frm.Show()
                    frm.Location = New Point(1, 1)
                Next
            End If


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

        EH.ProcessMessages(Me, Me.sbr, EH.ErrorMessage)
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub frm_Load(sender As Object, e As EventArgs) Handles frm.Load
        Try
            Dim f As Form = DirectCast(sender, Form)

            EH.ErrorMessage = String.Empty

	     f.?       ==============> I tried accessing one of the many controls on the form and none of them are exposed.
           
        Catch ex As Exception
            EH.ErrorMessage = "Form3/frm_Load() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, Me.sbr, EH.ErrorMessage)
    End Sub

Open in new window

BlakeMcKennaAsked:
Who is Participating?
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.

CodeCruiserCommented:
Similar to what was suggested in your other thread, you will need to maintain the reference to the form some where (class level variables or a list of controls or loop through the controls collection of tab).
0
Fernando SotoRetiredCommented:
A Form type has NO controls on it and therefore nothing to access from that type. You will need to cast it to the type of Form you want to access the controls on. Fro example if it is a Form2 type then

Dim f As Form = DirectCast(sender, Form2)

Which does have the Type of controls you want to access.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
It looks like you're actually asking how to access controls in the Load() event of those forms.  This is a much easier problem to solve.  Currently you're casting the "sender" parameter to the generic Form type, which does not have knowledge of the controls you added at design-time.  All you need to do is cast it to the correct type, which in your case is "frmLoadTest":
Private Sub frm_Load(sender As Object, e As EventArgs) Handles frm.Load
        Try
            Dim f As frmLoadTest  = DirectCast(sender, frmLoadTest) ' <-- cast to frmLoadTest instead of Form!

            EH.ErrorMessage = String.Empty

	     f.xxx = yyy ' <-- now it should find everything!
           
        Catch ex As Exception
            EH.ErrorMessage = "Form3/frm_Load() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, Me.sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

BlakeMcKennaAuthor Commented:
Fernando,

What you said makes sense, however, I tried what you suggested and intellisense did not pop up and controls when I went to access one. See screenshot.
Screenshot.jpg
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
See my post, you still have Form in there.

Change it to the correct type in BOTH places:
Dim f As frmLoadTest  = DirectCast(sender, frmLoadTest)

Open in new window

0

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:
That's what I needed...thanks Mike!
0
Fernando SotoRetiredCommented:
That is because the sender object in that event is a frm object and not a frmLoadTest object. As well as Mike's last post.
0
BlakeMcKennaAuthor Commented:
That makes sense now...thank you Fernando for explanation.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.