Checked ListBox Control in VB.Net

The CheckedListBox control in VB.Net provides all the features and functionality of a standard ListBox control, with the added capability of displaying a check mark alongside each item in the list. This additional functionality allows users to select multiple items from the list by checking the corresponding checkboxes.

vb.net-checkedlistbox.jpg

The CheckedListBox control enables users to toggle the selection state of each item independently by clicking on the associated checkbox. The state of each checkbox (checked or unchecked) can be programmatically accessed and manipulated to perform actions based on the user's selections.

AddRange method

To dynamically add objects to a CheckedListBox at runtime in VB.Net, you can utilize the AddRange method. This method allows you to assign an array of object references, which will then be displayed as items in the CheckedListBox.

By using the AddRange method, you can efficiently add multiple objects to the CheckedListBox in a single operation, rather than adding them one by one. The objects can be of any type, and the CheckedListBox will display the default string representation of each object.

Upon adding the objects, the CheckedListBox will automatically generate the default string value for each object. This string representation is typically obtained by calling the ToString method on each object. It's important to ensure that the objects in the array have a meaningful ToString implementation to display relevant information in the CheckedListBox.

Dim days As String() = {"Sunday", "Monday", "Tuesday"} checkedListBox1.Items.AddRange(days)

You can add individual items to the list with the Add method. The CheckedListBox object supports three states through the CheckState enumeration: Checked, Indeterminate, and Unchecked.

CheckedListBox1.Items.Add("Sunday", CheckState.Checked) CheckedListBox1.Items.Add("Monday", CheckState.Unchecked) CheckedListBox1.Items.Add("Tuesday", CheckState.Indeterminate)
Full Source VB.NET
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load CheckedListBox1.Items.Add("Sunday", CheckState.Checked) CheckedListBox1.Items.Add("Monday", CheckState.Unchecked) CheckedListBox1.Items.Add("Tuesday", CheckState.Indeterminate) CheckedListBox1.Items.Add("Wednesday", CheckState.Checked) CheckedListBox1.Items.Add("Thursday", CheckState.Unchecked) CheckedListBox1.Items.Add("Friday", CheckState.Indeterminate) CheckedListBox1.Items.Add("Saturday", CheckState.Indeterminate) End Sub End Class

Conclusion

By using the CheckedListBox control, you can provide a versatile and user-friendly interface for managing multiple selections within your VB.Net application.