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

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.