Solved

Check to see if the majority of checkboxes are checked on a form?

Posted on 2006-10-28
4
212 Views
Last Modified: 2010-04-23
I have over 30+ checkboxes on a form that I want to check to see if they're all checked and do something in which the code below works great. But I need to somehow change the code and not check for certain checkboxes.

For example checkboxes: chkNET, chkCIMS, chkLCD, chkCSI

should be excluded from the code below...

Dim AllChecked As Boolean = True

        For Each c As Control In Me.Controls
            If TypeOf c Is CheckBox Then
                If Not CType(c, CheckBox).Checked Then
                    AllChecked = False
                    Exit For
                End If
            End If
        Next

        If AllChecked Then
            '// All checkboxes are checked
            MessageBox.Show("AllChecked")
        Else
            MessageBox.Show("Not AllChecked")
        End If


thanks
0
Comment
Question by:rkckjk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17825244
If you're not using the .Tag for anything else, set it as True if you want to include the checkbox, and False otherwise, then

        For Each c As Control In Me.Controls
            If TypeOf c Is CheckBox Then
                If CType(c, CheckBox).Tag = True then
                    If Not CType(c, CheckBox).Checked Then
                        AllChecked = False
                        Exit For
                    End If
                End if
            End If
        Next

Or some variation on that theme.

Or, at form load (or whereever)

   Dim CheckThese As New Arraylist
   'code to add relevant checkboxes to CheckThese

Then

        For Each c As CheckBox In CheckThese
            If c.Checked Then
                AllChecked = False
                Exit For
            End If
        Next

Or some variation on that theme.

Roger
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 17825249
try it this way:
        Dim IChecked as Integer = 0
        Din iCount as Integer = 0
        For Each c As Control In Me.Controls
            If TypeOf c Is CheckBox Then
                iCount += 1
                If Not CType(c, CheckBox).Checked Then
                    AllChecked = False
                Else
                    iChecked += 1
                End If
            End If
        Next

       If AllChecked Then
            '// All checkboxes are checked
            MessageBox.Show("AllChecked")
        Else
            MessageBox.Show("Out of " & iCount & " checkboxes " & 100.0 *cDbl(iChecked)/cDbl(iCount) & " % are checked")
        End If


AW
0
 
LVL 2

Author Comment

by:rkckjk
ID: 17825252
Where do I set the .Tag for the checkboxes I want to include?
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17825312
In the IDE.  Click on the CheckBox: go to Tag in the poperties window: type True if you want it included and False if you don't.  Strictly, doing it that way, you ought to use string values rather than booleans, but True and False seem to work.  Or you could use "a" and "b" (or whatever) and adapt the code accordingly.

Otherwise, you would need to do it programmatically.

    ThisCheckBox.Tag = True
    ThatCheckBox.Tag = False
    'etc through all 30 ;-)

Roger
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question