How to check or uncheck all items in VB.NET CheckedListBox Control

I need to select and deselect all items in a VB.NET CheckedListBox , what is the best way to do this?

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load With clbCheckedListBox .Items.Add("Select/UnSelect All") .Items.Add("Enero") .Items.Add("Febrero") .Items.Add("Marzo") .Items.Add("Abril") .Items.Add("Mayo") .Items.Add("Junio") .Items.Add("Julio") .Items.Add("Agosto") .Items.Add("Septiembre") .Items.Add("Octubre") .Items.Add("Noviembre") .Items.Add("Diciembre") .SelectedIndex = 0 End With End Sub Private Sub clbCheckedListBox_ItemCheck(sender As Object, e As System.Windows.Forms.ItemCheckEventArgs) Handles clbCheckedListBox.ItemCheck If e.Index = 0 Then If e.NewValue = CheckState.Checked Then For idx As Integer = 1 To Me.clbCheckedListBox.Items.Count - 1 Me.clbCheckedListBox.SetItemCheckState(idx, CheckState.Checked) Next ElseIf e.NewValue = CheckState.Unchecked Then For idx As Integer = 1 To Me.clbCheckedListBox.Items.Count - 1 Me.clbCheckedListBox.SetItemCheckState(idx, CheckState.Unchecked) Next End If End If End Sub 

After an hour, the above code works great for me!

+6
source share
7 answers

Ricardo, maybe this may be what you are looking for:

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim items$() = New String() {"Select/UnSelect All", "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"} For Each Str As String In items : clbCheckedListBox.Items.Add(Str) : Next End Sub ' Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Private Sub clbCheckedListBox_ItemCheck(sender As System.Object, e As System.Windows.Forms.ItemCheckEventArgs) Handles clbCheckedListBox.ItemCheck If e.Index = 0 Then Dim newCheckedState As CheckState = e.NewValue For idx As Integer = 1 To clbCheckedListBox.Items.Count - 1 Me.clbCheckedListBox.SetItemCheckState(idx, newCheckedState) Next End If End Sub 
+3
source

You mean something like this:

 Dim checked As Boolean = True ' Set to True or False, as required. For i As Integer = 0 To CheckedListBox1.Items.Count - 1 CheckedListBox1.SetItemChecked(i, checked) Next 

Here I just go through all the elements of the CheckedListBox and set their state.

+14
source
 If button.Text = "Select All" Then For i As Integer = 0 To checklist.Items.Count - 1 checklist.SetItemChecked(i, True) Next Button.Text = "Deselect All" Else For i As Integer = 0 To checklist.Items.Count - 1 checklist.SetItemChecked(i, False) Button.Text = "Select All" Next End If 
+4
source
 To check all CheckedListBox Item: For i As Integer = 0 To CheckedListBox1.Items.Count - 1 CheckedListBox1.SetItemChecked(i, True) Next To uncheck all CheckedListBox Item: For i As Integer = 0 To CheckedListBox1.Items.Count - 1 CheckedListBox1.SetItemChecked(i, false) Next 
+1
source

I found that clbCheckedListBox.clearSelection() works great in order to deselect all.

+1
source

Added a separate checkbox called "Select All". When checking and unchecking these checkbox elements, checklistbox may or may not be selected. So you can call this Kb() function anywhere in your code:

 Private Sub ChkSelectAll_Click(sender As Object, e As EventArgs) Handles ChkSelectAll.Click Kb(ChkSelectAll.CheckState) End Sub Private Sub Kb(ByVal Key As Boolean) For i As Integer = 0 To ChkLstServices.Items.Count - 1 ChkLstServices.SetItemChecked(i, Key) Next End Sub 
0
source

Put this code in the SelectedValueChanged event.

 Private Sub clbCheckedListBox_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles ContrListCheckBox.SelectedValueChanged If clbCheckedListBox.SelectedIndex = 0 Then If clbCheckedListBox.GetItemChecked(0) = False Then For idx As Integer = 1 To clbCheckedListBox.Items.Count - 1 Me.clbCheckedListBox.SetItemChecked(idx, False) Next Else For idx As Integer = 1 To ContrListCheckBox.Items.Count - 1 Me.clbCheckedListBox.SetItemChecked(idx, True) Next End If ElseIf clbCheckedListBox.SelectedIndex > 0 Then If clbCheckedListBox.CheckedItems.Count = clbCheckedListBox.Items.Count - 1 And clbCheckedListBox.GetItemChecked(0) = False Then clbCheckedListBox.SetItemCheckState(0, CheckState.Checked) End If For idx As Integer = 1 To clbCheckedListBox.Items.Count - 1 If clbCheckedListBox.GetItemChecked(idx) = False Then clbCheckedListBox.SetItemCheckState(0, CheckState.Unchecked) End If Next End If End Sub 

Other solutions are true, but if you want to deselect a different checkbox inside CheckBoxList or remove CheckBoxList without the Select All checkbox, the top checkbox will remain on, and this is illogical, so the code above should solve this problem.

0
source

Source: https://habr.com/ru/post/909233/


All Articles