Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Many checkboxes (128) on form

Posted on 2013-06-26
2
Medium Priority
?
296 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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