Question Deselect All items in a Check List Box

Ja2984

Member
Joined
Nov 23, 2007
Messages
13
Programming Experience
Beginner
Hi all hope this is the correct forum to post in.

I have some code that selects all items in a check list box and i'm having trouble trying to writing some code to deselect all items.

Here is my code to select all:
VB.NET:
Private Sub GeoCheckItems(ByVal IsChecked As Boolean)

        GeoCheckListCount = Me.GeoCheckedListBox.Items.Count

        If GeoCheckListCount >= 1 Then

            If Me.GeoCheckBox.Checked = True Then
                Me.GeoProgressBar.Value = 0
                For Counter As Integer = 0I To Me.GeoCheckedListBox.Items.Count - 1I
                    Me.GeoCheckedListBox.SetItemChecked(Counter, IsChecked)
                Next
                Me.GeoProgressBar.Value = 100
            End If
        End If
    End Sub

Theres probably a simple solution but i just cant find it and its driving me crazy :confused:

Thanks for any suggestions
 
You could simply change your Sub to apply both scenarios as shown below:

NOTES:

CheckBox1 = Checkbox to Check/Uncheck All


VB.NET:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

        Dim CheckListCount As Integer = Me.CheckedListBox1.Items.Count

        If CheckListCount >= 1 Then

            If Me.CheckBox1.Checked = True Then

                For Counter As Integer = 0I To Me.CheckedListBox1.Items.Count - 1I
                    Me.CheckedListBox1.SetItemChecked(Counter, Me.CheckBox1.Checked)
                Next
   
            End If
        End If

    End Sub

Let me know if it worked for you.
Regards.

Ricardo Vercesi
 
All you need is this:
VB.NET:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    For Counter As Integer = 0I To Me.CheckedListBox1.Items.Count - 1I
        CheckedListBox1.SetItemChecked(Counter, CheckBox1.Checked)
    Next Counter
End Sub
 
hi guys

Thanks for the suggestions. Both worked fine.

Just curious, can i create a sub procedure of its own and then call it using a button instead of using the check box?

E.g. i want to have a button so that when i click on it with the left mouse button it Selects all and if i click it with the right mouse button it deselects all.

VB.NET:
If mouseclick = mousebuttons.right then
 call deselectAll (sender, e)
end if
if mouseclick = mousebuttons.left then
 call selectAll (sender, e)
end if

I know thats not correct in any shape or form but hopefully u get the idea.:eek:
 
Here's how I would do this:
VB.NET:
Private Sub CheckItems(Byval isChecked As Boolean)
    If Me.CheckedListBox1.Items.Count > 0I Then
        For Counter As Integer = 0I To Me.CheckedListBox1.Items.Count - 1I
            CheckedListBox1.SetItemChecked(Counter, isChecked)
        Next Counter
    End If
End Sub
Now all you need is to call the sub and pass it a True or False if it's going to check or uncheck all of the items. So setting it up looking for a left or right mouse click is easy:
VB.NET:
CheckItems(mouseclick <> mousebuttons.right)
What that does is if "mouseclick" doesn't equal the right mouse button then a "True" is passed to the sub, otherwise it's "False"
 
Toggle a button to check or uncheck all boxes.

Use a single button to either check all or uncheck all checkboxes in a checked listbox:
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static onoff As Boolean
        onoff = Not onoff
        For x As Integer = 0 To CheckedListBox1.Items.Count - 1
            If onoff = True Then
                CheckedListBox1.SetItemChecked(x, True)
                Button1.Text = "Uncheck All"
            ElseIf onoff = False Then
                CheckedListBox1.SetItemChecked(x, False)
                Button1.Text = "Check All"
            End If
        Next
    End Sub
 
Use a single button to either check all or uncheck all checkboxes in a checked listbox:
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static onoff As Boolean
        onoff = Not onoff
        For x As Integer = 0 To CheckedListBox1.Items.Count - 1
            If onoff = True Then
                CheckedListBox1.SetItemChecked(x, True)
                Button1.Text = "Uncheck All"
            ElseIf onoff = False Then
                CheckedListBox1.SetItemChecked(x, False)
                Button1.Text = "Check All"
            End If
        Next
    End Sub
What if the user want the opposite of what's given first on the button? This obviously isnt a critical question, but end users tend to be nit picky about stuff like this.
 
Then just click the button twice, or use two separate buttons.
Alternatively, you can use a checkbox:

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For x As Integer = 0 To CheckedListBox1.Items.Count - 1
            If CheckBox1.Checked = True Then
                CheckedListBox1.SetItemChecked(x, True)
            ElseIf CheckBox1.Checked = False Then
                CheckedListBox1.SetItemChecked(x, False)
            End If
        Next
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            Button1.Text = "Check All"
        ElseIf CheckBox1.Checked = False Then
            Button1.Text = "Uncheck All"
        End If
    End Sub
 
Last edited:
Back
Top