check all checkboxes

This doesn't seem to be working for me...what am I missing?

        Dim ctl As CheckBox
        For Each ctl In Controls
           ctl.Checked = True
        Next
LVL 67
sirbountyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Dim cb As CheckBox
Dim ctl As Control
For Each ctl In Me.Controls
    If TypeOf ctl Is CheckBox Then
        cb = CType(ctl, CheckBox)
        cb.Checked = True
    End If
Next


*** BUT *** this will only hit checkboxes directly contained by the FORM itself.  If the checkboxes are contained in another control (panel, groupboxes, etc) then you would need a recursive algorithm to find all of them.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sirbountyAuthor Commented:
ksir - can ya explain what we're doing here?  I'm not very familiar with ctype...does that statement convert the control to a checkbox before proceeding?  Why doesn't the fact that I've established ctl as a checkbox work?  Shouldn't the loop go through all checkboxes, since ctl isn't declared as a generic control? : \
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
When you use Me.Controls, you iterate ALL controls directly contained by the form.  What if you have controls on your form that are not CheckBoxes?  This would cause an error since your code would attempt to convert say a TextBox to a CheckBox in your For...Next loop!

You are correct about the CType() function.  It is converting the "ctl" variable to a CheckBox if it passed the "TypeOf ctl Is CheckBox" test (which returns True if "ctl" is a CheckBox).
sirbountyAuthor Commented:
The way I read it was
Declare a variable as a checkbox control.  Now loop through all of those 'type' controls, not 'all' controls.
I guess it sort of makes sense, I just presumed that by declaring the ctl as a specific checkbox that that would/should limit the iteration to those types of controls only...
Thanx for the follow.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Don't feel bad, your assumption is a pretty common one and the type of code you posted comes up on EE quite a bit.  =)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.