CheckedListBox Control in VB.NET

CheckedListBox Control in VB.NET.
  • 9631
CheckedListBox Control 
The Windows Forms CheckedListBox control displays a list of items, like the ListBox control, and also can display a check mark next to items in the list.

Image of the ChechListBox Control :

The Windows Forms CheckedListBox control extends the ListBox control. It does almost everything that a list box does and also can display a check mark next to items in the list. Other differences between the two controls are that checked list boxes only supportDrawMode.Normal; and that checked list boxes can only have one item or none selected. Note that a selected item appears highlighted on the form and is not the same as a checked item.

Checked list boxes can have items added at run time using the String Collection Editor or their items can added dynamically from a collection at run time, using the Items property.

Determining Checked Items in the Windows Forms CheckedListBox Control
When presenting data in a Windows Forms CheckedListBox control, you can either iterate through the collection stored in the CheckedItems property, or step through the list using theGetItemChecked method to determine which items are checked. The GetItemChecked method takes an item index number as its argument and returns true or false. Contrary to what you might expect, the SelectedItems and SelectedIndices properties do not determine which items are checked; they determine which items are highlighted.

To determine checked items in a CheckedListBox control

  • Iterate through the CheckedItems collection, starting at 0 since the collection is zero-based. Note that this method will give you the item number in the list of checked items, not the overall list. So if the first item in the list is not checked and the second item is checked, the code below will display text like "Checked Item 1 = MyListItem2".

       Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
            If CheckedListBox1.CheckedItems.Count <> 0 Then
                ' If so, loop through all checked items and print results.
                Dim x As Integer
                Dim s As String = ""
                For x = 0 To CheckedListBox1.CheckedItems.Count - 1
                    s = s & "Checked Item " & (x + 1).ToString & " = " & CheckedListBox1.CheckedItems(x).ToString & ControlChars.CrLf
                Next x
            End If
  •     End Sub

    Output :





More Articles

© 2020 DotNetHeaven. All rights reserved.