Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-10-28
4
Medium Priority
?
215 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
[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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

722 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