|
-
Apr 22nd, 2008, 02:59 PM
#1
Thread Starter
PowerPoster
[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,
-
Apr 22nd, 2008, 03:28 PM
#2
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.
-
Apr 22nd, 2008, 04:40 PM
#3
Thread Starter
PowerPoster
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
-
Apr 22nd, 2008, 05:20 PM
#4
Lively Member
Re: [2005] GroupBox Container issue?
This should work......
Code:
CType(ctrl, CheckBox).Checked = True
Cheers
Microsoft Visual Basic 2008
-
Apr 22nd, 2008, 05:30 PM
#5
Thread Starter
PowerPoster
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,
-
Apr 22nd, 2008, 05:32 PM
#6
Thread Starter
PowerPoster
Re: [2005] GroupBox Container issue?
nevermind, I saw my problem. SunglassesRon, your code worked to.
Thanks,
-
Apr 22nd, 2008, 05:55 PM
#7
Lively Member
Re: [2005] GroupBox Container issue?
You don't need this line...
Code:
ctl = DirectCast(ctl, CheckBox)
Microsoft Visual Basic 2008
-
Apr 22nd, 2008, 06:20 PM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|