Solved

Many checkboxes (128) on form

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicating a tab page content to another in TabControl control 11 44
Convert an ASPX page into PDF 7 32
VB.NET 1 22
write xml in vb.net 2 24
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 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