Solved

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

Posted on 2006-10-28
4
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
Where do I set the .Tag for the checkboxes I want to include?
0
 
LVL 34

Expert Comment

by:Sancler
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now