Solved

Many checkboxes (128) on form

Posted on 2013-06-26
2
287 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
[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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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