Search code examples
vb.netcheckboxgroupbox

VB - Unchecking Check Boxes in a Group Box


Currently I have 5 group boxes all filled with checkboxes, when I want to unselect all of them (for a 'clear selection' button), I use this code that I found on a forum:

For Each CheckBox In grpbox_Hiragana
        CheckBox.checked = "false"

Firstly, I'm sure if this is the correct way to unselect the checkboxes, secondly the "grpbox_Hiragana" groupbox returns the following error:

Expression is of type 'System.Windows.Forms.GroupBox', which is not a collection type

If anyone could confirm this is the correct way of doing this/ help fix the error by telling me why the groupbox won't be accepted that would be great.


Solution

  • if you have all check box on one group box use this code :

        Dim ChkBox As CheckBox = Nothing
        ' to unchecked all 
        For Each xObject As Object In Me.GroupBox1.Controls
            If TypeOf xObject Is CheckBox Then
                ChkBox = xObject
                ChkBox.Checked = False
            End If
        Next
    
       ' to checked all 
        For Each xObject As Object In Me.GroupBox1.Controls
            If TypeOf xObject Is CheckBox Then
                ChkBox = xObject
                ChkBox.Checked = True
            End If
        Next
    

    Or you can use CheckedListBox Control.