Solved

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

Posted on 2006-10-28
4
210 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
  • 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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