Working with Forms and Controls in VB.NET: CheckedListBox Control
The CheckedListBox control in VB.NET is a powerful control that allows users to select multiple items from a list. Each item in the list is displayed with a checkbox that the user can select or deselect. In this tutorial, we will learn how to work with the CheckedListBox control in VB.NET.
Syntax
The CheckedListBox control can be added to a VB.NET form in the Visual Studio IDE by dragging and dropping it from the Toolbox. The control can also be added programmatically using the following syntax:
Dim clb As New CheckedListBox
Me.Controls.Add(clb)
Example
In this example, we will add a CheckedListBox control to a form and populate it with items at run time.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckedListBox1.Items.Add("Item 1")
CheckedListBox1.Items.Add("Item 2")
CheckedListBox1.Items.Add("Item 3")
End Sub
End Class
Output
The output of the above example will be a form with a CheckedListBox control that contains three items.
Explanation
In the above example, the Form1_Load
event is used to populate the CheckedListBox control with items at runtime. The Items.Add
method is used to add three items to the CheckedListBox control.
Use
The CheckedListBox control is useful in situations where users need to select multiple items from a list. It is commonly used in software applications that require the user to select multiple options, such as in a multi-select filter or in a list of items that can be added or removed.
Important Points
- The CheckedListBox control allows users to select multiple items from a list.
- The
Items.Add
method is used to add items to the CheckedListBox control at runtime. - The state of the checkboxes can be accessed using the
CheckedItems
property.
Summary
In summary, the CheckedListBox control is a powerful tool in VB.NET that allows users to select multiple items from a list. It is useful in a variety of situations, such as in a multi-select filter or in a list of items that can be added or removed. The Items.Add
method is used to add items to the CheckedListBox control at runtime, and the state of the checkboxes can be accessed using the CheckedItems
property.