For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
            If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
                For Each subCtrl As Control In ctrl.Controls
                    If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
                        MsgBox(subCtrl.Text)
                    End If

                    If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) Then
                        If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) Then                             
                            If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then
                                MsgBox("Male")
                            End If
                            If CType(subCtrl.Controls("rbttnF"), RadioButton).Checked Then
                                MsgBox("Female")
                            End If
                        End If
                    End If
                Next
        End If
        Next

Recommended Answers

All 4 Replies

Code breaks (error message) at If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton)

An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an object.

Set the code in this other way and see if it still throws an exception:

        For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
            If ctrl.GetType Is GetType(Windows.Forms.Panel) Then
                Select Case ctrl.GetType
                    Case GetType(TextBox)
                        ' do whatever '
                    Case GetType(ComboBox)
                    Case GetType(RadioButton)
                        Dim r As RadioButton = CType(ctrl, RadioButton)
                        If r.Name = "rbttnM" AndAlso r.Checked Then
                            ' do whatever '
                        ElseIf r.Name = "rbttnF" AndAlso r.Checked Then
                            ' do whatever '
                        End If
                End Select
            End If
        Next

Use of Select Case as mentioned by xrj might be enough to find the issue. (Although I wonder about the use of ctrl.GetType versus subCtrl.GetType. Perhaps it's xrj's attempt to force you to think for yourself ....)
But I think it doesn't matter whether you prefer Select Case or if then / elseif /elseif / else / end if at all, the construction

8.               If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) Then
9.                   If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) Then

can't ever be correct. You have nested two if's while both check for the type of the same object. If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) is true then subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) is always false and 'vice versa'. In the code snipped you (Enrique_2) have posted, there's actually no reason to use the check subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.