I think Inferrd got a point when he says that the Radiobuttons might actually not be where you think they are.
JMC have posted a nice snippet in this thread that iterates through every sub control. Based on that example, what happen if you change your function to this?
vb.net Code:
Private Function WhatRadioIsSelected(ByVal MainPanel As Panel) As String 'Note that it is iterating through the main panel controls and its child controls
Dim result As String = Nothing
Dim ctl As Control = MainPanel.GetNextControl(MainPanel, True) 'Get the first control in the tab order.
Do Until ctl Is Nothing
If TypeOf ctl Is GroupBox Then
For Each ControlInGroupBox As Control In DirectCast(ctl, GroupBox).Controls.OfType(Of RadioButton)
If DirectCast(ControlInGroupBox, RadioButton).Checked = True Then result = ControlInGroupBox.Text
Next
End If
ctl = MainPanel.GetNextControl(ctl, True) 'Get the next control in the tab order.
Loop
Return result
End Function
Instead of using the groupbox as a parameter, use the main panel instead.
vb.net Code:
Messagebox.Show(WhatRadioIsSelected(pnlMainPanel))
Also I agree with Sytten, using a Usercontrol will result in a much lighter coding experience, and in my opinion adding and removing several controls and their event handlers in run time should only be used in special cases. I include a project related to your issue (which is slightly overkill for demonstrating purposes) where everything is designed in run time. The project only got a few controls, but image if there where tens or hundreds of them
You can compare for yourself why the Usercontrol example is a good example