简体   繁体   中英

Single checkbox to check multiple checkboxes in Visual Basic

Hi good people of stackoverflow, I have created a groupBox1 which contains 39 checkboxes which can be checked individually, and I have created a gropuBox2 which contains single check box called "Check All", What I want is when the "Check All" check box is checked all the 39 checkboxes will be selected and when the "Check All" is unchecked all 39 checkboxes will be unchecked also, Can someone please help? Thank you very much.

If all your controls are in the form ( not in groupbox/pannel) you can use the following code

  Dim chk As CheckBox
  If checkAll.Checked = True Then
     For Each ctrl As Control In Me.Controls
         If TypeOf ctrl Is CheckBox Then
            chk = DirectCast(ctrl, CheckBox)
            chk.Checked = True
         End If
     Next
   End If

Updates as per edit and comment:

If you want to find controls inside Groupbox1 means you can do like the following

''' <summary>
''' sets CheckBox.Checked inside a container
''' </summary>
''' <param name="parentControl">the container</param>
''' <param name="chkState">ture or false</param>
''' <remarks>use: checkControls(GroupBox1, True)</remarks>
Public Sub checkControls(ByVal parentControl As Control, chkState As Boolean) '<----Pass the parent control where the checkBoxes belongs
    Dim chk As CheckBox = Nothing
    For Each ctrl As Control In parentControl.Controls '<-----Change from the above code
        If TypeOf ctrl Is CheckBox Then
            DirectCast(ctrl, CheckBox).Checked = chkState
        End If
    Next
End Sub

To check call the function as

  checkControls(GroupBox1, True)'<----Since your checkboxes are inside the groupBox1

To uncheck call the function as

  checkControls(GroupBox1, False)'<----Since your checkboxes are inside the groupBox1

Assuming that there are only 40 checkboxes on the form then this will work

    Dim ctrl As Control = Me.GetNextControl(Me, True)
    Do Until ctrl Is Nothing
        If TypeOf ctrl Is CheckBox Then
            DirectCast(ctrl, CheckBox).Checked = True
        End If
        ctrl = Me.GetNextControl(ctrl, True)
    Loop

If there are more than 40 checkboxes then take a look at Neethu Soman's solution.

Here's how I would do it. Filter the controls by type using Enumerable.OfType<T> then iterate the result using Array.ForEach<T> .

Private Sub CheckBoxAll_CheckedChanged(sender As Object, e As EventArgs) Handles CheckAll.CheckedChanged
    Array.ForEach(Me.GroupBox1.Controls.OfType(Of CheckBox).ToArray(), Sub(box As CheckBox) box.Checked = Me.CheckAll.Checked)
End Sub

I've tried the code below and works for me but Bjørn-Roger Kringsjå code works much better:

Private Sub cbxAll_CheckedChanged(sender As Object, e As System.EventArgs) Handles cbxAll.CheckedChanged                                                                      
If cbxAll.Checked = True Then
    cbx01.Checked = True
    cbx02.Checked = True
    ' 3 .. 38
    cbx39.Checked = True
Else
    cbx01.Checked = False
    cbx02.Checked = False
    ' 3 .. 38
    cbx39.Checked = False
End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM