ARTICLE

CheckedListBox Control in VB.NET

Posted by Dinesh Beniwal Articles | Windows Forms VB.NET August 31, 2009
CheckedListBox Control in VB.NET.
Download Files:
 
Reader Level:

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 :

check1.gif
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 support DrawMode.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 the GetItemChecked 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
                MessageBox.Show(s)
            End If

        End Sub

    Output :

    check2.gif

Login to add your contents and source code to this article
share this article :
post comment
 

Good it is very simple

Posted by Shiva Guru Sep 13, 2010

hi i have a question for you. i have a checked list box. and a button. i want to make checked list box readonly when i press that button

Posted by abhi vs Jan 03, 2010
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor