[RESOLVED] [2005] GroupBox Container issue?
I have a Groupbox control on my form in which I have about 8 checkbox controls. One of the checkboxes, if checked, will make the rest of the checkboxes become checked. I have the following code that isn't working.
Code:
For Each ctl As Control In gb1.Container.Components
If TypeOf (ctl) Is CheckBox Then
ctl.Checked = True 'Isn't working
End If
Next
With what I have above, "Checked" isn't a property of the "ctl". What am I doing wrong in this construct?
Thanks,
Re: [2005] GroupBox Container issue?
Once you find that your Control is of type CheckBox, you have to cast that Control to an actual CheckBox. You don't actually have a CheckBox until you cast that Control to a CheckBox.
You get "Checked" isn't a property of the "ctl" because Checked really isn't a property of Control. Checked is a property of a CheckBox.
Re: [2005] GroupBox Container issue?
nmadd,
I did what you suggested and still got an error, unless I did it wrong! Here's the code:
Code:
For Each ctl As Control In gb1.Container.Components
If TypeOf (ctl) Is CheckBox Then
ctl = DirectCast(ctl, CheckBox)
ctl.checked = True 'Doesn't like this statement
End If
Next
Re: [2005] GroupBox Container issue?
This should work......
Code:
CType(ctrl, CheckBox).Checked = True
Cheers
Re: [2005] GroupBox Container issue?
Ok,
Now I'm getting the attached error message on the highlighted line:
Code:
For Each ctl As Control In gb1.Container.Components
If TypeOf (ctl) Is CheckBox Then
ctl = DirectCast(ctl, CheckBox)
CType(ctl, CheckBox).Checked = False
End If
Next
Thanks,
Re: [2005] GroupBox Container issue?
nevermind, I saw my problem. SunglassesRon, your code worked to.
Thanks,
Re: [2005] GroupBox Container issue?
You don't need this line...
Code:
ctl = DirectCast(ctl, CheckBox)
Re: [2005] GroupBox Container issue?
On a side note, it won't make much difference unless you're doing this a million times, but if you are certain that you are casting to the right type, DirectCast is faster.