Hello

I have an array of boolean data that I'm trying to display on the screen using 144 check boxes. Is there an easy way of updating all 144 check boxes with the data from an array and an easy way to update the array when changes are made to the check boxes?

Thanks for any help


Simon

Dani AI

Generated

Short summary and practical pattern (builds on and ):

A single shared CheckedChanged handler attached at runtime keeps the code tidy and immediate; use AddHandler to wire every CheckBox to the same handler so state changes update a single boolean array. (learn.microsoft.com)

Map each visual CheckBox to an array index by using its Tag property (set at design-time or in Form_Load) and attach the handler with AddHandler. When initializing the UI, set an isLoading flag so programmatic changes do not trigger array writes. If checkboxes live inside GroupBoxes, Panels, etc., enumerate controls recursively rather than iterating Me.Controls once; that ensures none are missed. (learn.microsoft.com)

Example pattern (VB.NET):

' Form-level
Private flags(143) As Boolean
Private isLoading As Boolean = False

' In Form_Load: assign Tag and handler
For i = 0 To 143
  Dim cb As CheckBox = GetCheckBoxByIndex(i) ' assign or find controls and set Tag
  cb.Tag = i
  AddHandler cb.CheckedChanged, AddressOf CheckBox_CheckedChanged
Next

Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs)
  If isLoading Then Return
  Dim cb = DirectCast(sender, CheckBox)
  flags(CInt(cb.Tag)) = cb.Checked
End Sub

For storage and alternatives: use System.Collections.BitArray to hold 144 bits compactly (easy to copy to bytes for file/settings storage), or switch the UI to a CheckedListBox if a single scrollable list is acceptable — CheckedListBox simplifies bulk operations and has built‑in checked-item helpers. (learn.microsoft.com)

Troubleshooting tips: use an isLoading boolean to suppress events during load, call SuspendLayout/ResumeLayout (or BeginUpdate/EndUpdate for list controls) while changing many controls, persist the boolean array as bytes or JSON (avoid fragile name-delimited strings), and use a consistent naming/tagging convention so mapping remains reliable across future changes.

Recommended Answers

All 3 Replies

I'd write a global event handler for all the checkboxes to make it instant, else you'd need a sub for each checkbox to either update the array OR call a function to do it, meaning messy code.

Might be worth having a quick flick through this techicle;

See if this helps.

'//----- Pre-requisites: 3 Buttons, and a TON of CheckBoxes. ----------\\
Public Class Form1
    Private arCB() As String = Nothing '// string Array to store the CheckBoxes Names and CheckStates.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "save"
        Button2.Text = "clear"
        Button3.Text = "load"
    End Sub

    '// Save Names and CheckStates as Arrays to arCB.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sTemp As String = "" '// string to add Names and CheckStates to Split into Arrays.
        For Each ctl As Control In Me.Controls '// loop thru all Controls.
            If TypeOf (ctl) Is CheckBox Then '// check if Control Is CheckBox.
                Dim cb As CheckBox = ctl '// declare CheckBox to get CheckState.
                sTemp &= "~" & cb.Name & "#" & cb.CheckState '// add CheckBox.Name and "#" and CheckBox.CheckState
                '//--- "#" is used to split Array again to extract CheckBox.Name and CheckBox.CheckState
            End If
        Next
        arCB = sTemp.Split("~") '// add each CheckBox.Name and CheckBox.CheckState as a Array.
    End Sub

    '// clear CheckBoxes.CheckStates. -- TESTING PURPOSES ONLY.
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        For Each ctl As Control In Me.Controls
            If TypeOf (ctl) Is CheckBox Then
                Dim cb As CheckBox = ctl
                cb.Checked = False
            End If
        Next
    End Sub

    '// Load CheckStates from arCB.
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '//-- start with Index 1 since Index 0 is Empty.
        For i As Integer = 1 To arCB.Length - 1 '// loop thru all Arrays in arCB.
            Dim arTemp() As String = arCB(i).Split("#") '// split Array into 2 Arrays, the Name and CheckState of CheckBox.
            For Each ctl As Control In Me.Controls '// loop thru all Controls.
                If TypeOf (ctl) Is CheckBox Then '// check if Control Is CheckBox.
                    Dim cb As CheckBox = ctl '// declare CheckBox to set CheckState.
                    If cb.Name = arTemp(0) Then '// if CheckBox.Name is first Array.
                        cb.Checked = arTemp(1) '// set the CheckState for the CheckBox from the second Array.
                        Exit For '// exit since Control was located and dealt with.
                    End If
                End If
            Next
        Next
    End Sub
End Class

Thanks for all the help :-) I'm using code from both replies :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.