0
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
3
Contributors
4
Replies
21
Views
3 Months
Discussion Span
Last Post by Joris Claassen
0

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

0

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

0

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
0

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.

Edited by Joris Claassen

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.