0

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    Dim txtName As New TextBox()
    For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
        If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
            For Each ctrl2 As Control In Me.Controls("PanelM").Controls
                If Me.Controls.GetType Is GetType(System.Windows.Forms.TextBox) Then
                    MsgBox(txtName.Text.ToString)
                End If
            Next
        End If
    Next
End Sub
Enrique Bello
  • 25
  • 1
  • 1
  • 8
  • Your ctrl2 variable is never used. Is PanelM a child of the form, or another panel? If another panel, then it would never be in the `Me.Controls` collection. – LarsTech Jun 15 '17 at 22:53
  • Trying to fish a control out of a collection by name can result in Nothing being returned. You need to check and possibly use the right collection – Ňɏssa Pøngjǣrdenlarp Jun 15 '17 at 22:57
  • I have a Main Panel and inside it a Panel with controls. One is a TexBox. Need to get the text in the TextBox. – Enrique Bello Jun 15 '17 at 23:03
  • Did you create these panels and controls with the designer? If so, it would be just `MessageBox.Show(txtName.Text)`. No loops. – LarsTech Jun 15 '17 at 23:11
  • I created this panels with controls in the child panel in a Dynamic form at run time and everything is ok except this problem.Can´t get the text in the TexBox – Enrique Bello Jun 15 '17 at 23:15
  • Try `For Each ctrl2 As Control In ctrl.Controls("PanelM").Controls`. You could just use the `Me.Controls.Find` function and look for the txtName control. txtName isn't referencing anything. – LarsTech Jun 15 '17 at 23:20
  • Lars Tech. Get the same error : Object reference not set to an instance of an object. – Enrique Bello Jun 15 '17 at 23:49
  • Side Note: `Me.Controls.GetType` is never ever going to be a Textbox type. Please read [ask] and take the [tour] ... or are you waiting until the system limits how often you can ask a question? – Ňɏssa Pøngjǣrdenlarp Jun 16 '17 at 01:07

0 Answers0