Solved

Many checkboxes (128) on form

Posted on 2013-06-26
2
282 Views
Last Modified: 2013-06-27
I have a form with 128 checkboxes that represent 128 fields in a bitmap.
Is there an easy way to detect when any of them have been  checked or unchecked?
Right now I have 128 Public Sub Checkbox?_CheckChanged events where ? is the checkbox number.

I have this code, I read something about a custom event handler for the checkboxes, but dont understand how to make it work.

Public Sub setCheckBoxes1(ByVal strBinary As String)
        Try
            Dim cControl As Control
            Dim tmpBinary As String = Replace(strBinary, " ", "")
            Dim intLength As Integer = Len(tmpBinary)
            For Each cControl In Me.Controls
                Dim z As Integer = 0 ' FieldName # converted to int
 
                If (TypeOf cControl Is CheckBox) And cControl.Tag = "Bitmap1" Then
                    Dim tmpBit() As String ' Make an array to store the FieldName and number
                    tmpBit = Split(cControl.Name, "CheckBox") ' Split the field name and number into an array
                    z = tmpBit(1) ' store the field number as an int
 
                    For x As Integer = 1 To intLength  ' Length of string with 0s and 1s , usually 64
                        If Mid(tmpBinary, x, 1) = 1 Then ' check to see if the value in strBinary is 1
                            CType(Me.Controls("CheckBox" & (x)), CheckBox).BackColor = Color.Red
                            CType(Me.Controls("CheckBox" & (x)), CheckBox).Checked = True
                        Else
                            CType(Me.Controls("CheckBox" & (x)), CheckBox).BackColor = Color.Blue
                            CType(Me.Controls("CheckBox" & (x)), CheckBox).Checked = False
                        End If
                    Next
                End If
            Next cControl
        Catch
        End Try
    End Sub

Thanks
MW
Public Sub setCheckBoxes1(ByVal strBinary As String)
        Try
            Dim cControl As Control
            Dim tmpBinary As String = Replace(strBinary, " ", "")
            Dim intLength As Integer = Len(tmpBinary)
            For Each cControl In Me.Controls
                Dim z As Integer = 0 ' FieldName # converted to int
 
                If (TypeOf cControl Is CheckBox) And cControl.Tag = "Bitmap1" Then
                    Dim tmpBit() As String ' Make an array to store the FieldName and number
                    tmpBit = Split(cControl.Name, "CheckBox") ' Split the field name and number into an array
                    z = tmpBit(1) ' store the field number as an int
 
                    For x As Integer = 1 To intLength  ' Length of string with 0s and 1s , usually 64
                        If Mid(tmpBinary, x, 1) = 1 Then ' check to see if the value in strBinary is 1
                            CType(Me.Controls("CheckBox" & (x)), CheckBox).BackColor = Color.Red
                            CType(Me.Controls("CheckBox" & (x)), CheckBox).Checked = True
                        Else
                            CType(Me.Controls("CheckBox" & (x)), CheckBox).BackColor = Color.Blue
                            CType(Me.Controls("CheckBox" & (x)), CheckBox).Checked = False
                        End If
                    Next
                End If
            Next cControl
        Catch
        End Try
    End Sub

Open in new window

0
Comment
Question by:WINN2012
2 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39278061
Well, you can set a common and unique event handler for all the 128 checkboxes. Assuming that all the CheckBoxes are placed over the same container (for example the Form itself), you can do this:

For Each c As Control In Me.Controls
    If TypeOf c Is CheckBox Then
        AddHandler DirectCast(c, CheckBox).CheckedChanged, AddressOf CheckedChanged
    End If
Next

Private Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    'When ANY of your checks become checked or unchecked, this code will execute
    'You can look what check is the origin of the event in the "sender" param
    'For example you can check DirectCast(sender, CheckBox).Name
End Sub

Open in new window


Hope that helps.
0
 

Author Closing Comment

by:WINN2012
ID: 39281062
Thats perfect, I now have a much better understanding of how addhandler works.
I can already see other areas in my app that would benefit from addhandler.

Tyvm

MW
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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.
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

17 Experts available now in Live!

Get 1:1 Help Now