Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-10-28
4
Medium Priority
?
216 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:Richard Kreidl
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 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:Richard Kreidl
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

916 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