[RESOLVED] [02/03] Using the Controls Collection of a groupbox?
Hi all, I am trying to uncheck the currently checked radiobutton in a groupbox with 6 radiobuttons. I have this:
VB Code:
Dim RB As RadioButton
For Each RB In GroupBox4.Controls
RB.Checked = False
Next
I get "Specified cast is not valid" in line "For Each RB In GroupBox4.Controls". What am I doing wrong, the editor isn't showing any errors.
Re: [02/03] Using the Controls Collection of a groupbox?
You can't cast every control as a RadioButton if not every control IS a RadioButton. You need to check the type and only if it is a RadioButton can you then cast it as such and set its Checked property:
VB Code:
For Each ctl As Control In GroupBox4.Controls
If TypeOf ctl Is RadioButton Then
DirectCast(ctl, RadioButton).Checked = False
End If
Next ctl
Re: [02/03] Using the Controls Collection of a groupbox?
Quote:
Originally Posted by jmcilhinney
if not every control IS a RadioButton.
haha John.. you must be coding too much.. you are starting to write your sentences in VB syntax :bigyello:
Re: [02/03] Using the Controls Collection of a groupbox?
Re: [02/03] Using the Controls Collection of a groupbox?
Ahh, I thought I can filter the collection further by providing what controls I am interested in. Thanks again, jmcilhinney (You must spread some Reputation around before giving it to jmcilhinney again :wave: )