0

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.

7
  • 1
    is the 39 checkboxes are located inside any control or they were stand alone in the form? Commented Oct 15, 2014 at 10:06
  • Yes, the 39 controls are inside a groupbox1 and the checkAll checkbox is inside groupBox2 Commented Oct 15, 2014 at 10:26
  • Are there any other checkboxes on the form anywhere? Commented Oct 15, 2014 at 11:09
  • Cool Neethu Soman, Thanks it works very good, if I want to the 39 unchecked when the checkAll is checked, I just need to reverse your code bellow? Apologies for this, I should have entered this part on my initial question. Commented Oct 15, 2014 at 11:21
  • You could add another argument to the checkControls method that is a boolean representing the checked state desired. Commented Oct 15, 2014 at 11:28

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.