Link to home
Start Free TrialLog in
Avatar of WINN2012
WINN2012Flag for United States of America

asked on

Many checkboxes (128) on form

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

ASKER CERTIFIED SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WINN2012

ASKER

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