简体   繁体   中英

Check/DeCheck all items in Check-list box vb.net

A couple of questions about check list boxes:

  1. How to check/decheck all the item in the list

  2. How do you copy or delete all checked items in the list

While you can scan the CheckedItems and CheckedIndices properties, you cannot modify them at the same time. Make a copy of the collection and process the copy instead.

Uncheck all checked items:

Dim CheckedIndices(CheckedListBox1.CheckedItems.Count - 1) As Integer
CheckedListBox1.CheckedIndices.CopyTo(CheckedIndices, 0)
For Each Index As Integer In CheckedIndices
    CheckedListBox1.SetItemChecked(Index, False)
Next

Delete all checked items:

Dim CheckedItems(CheckedListBox1.CheckedItems.Count - 1) As Object
CheckedListBox1.CheckedItems.CopyTo(CheckedItems, 0)
For Each CheckedItem As Object In CheckedItems
    ' Alternateively, add code to copy CheckedItem here.
    CheckedListBox1.Items.Remove(CheckedItem)
Next

EDIT: Changed array declarations to (.Count - 1). VB's declaration syntax in infuriating sometimes.

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