VB.NET: CheckedListBox Control Sample Project

Design UI

Code
 Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        CheckedListBox1.Items.Add("Red", False)
        CheckedListBox1.Items.Add("Orange",
False)
        CheckedListBox1.Items.Add("Yellow",
False)
        CheckedListBox1.Items.Add("Green",
False)
        CheckedListBox1.Items.Add("Blue",
False)
        CheckedListBox1.Items.Add("Black",
False)
        CheckedListBox1.Items.Add("Purple",
False)
        CheckedListBox1.Items.Add("Pink",
False)

        Button1.Text = "Get Items Status"
        Button2.Text = "Selected Count"
        Button3.Text = "Checked All"
        Button4.Text = "Unchecked All"
    End Sub

    Private Sub Button1_Click(ByVal sender
As System.Object, _
       
ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String = ""
        For i As Integer = 0 To CheckedListBox1.Items.Count - 1
            If CheckedListBox1.GetItemChecked(i) = True Then
                str = str & CheckedListBox1.Items(i).ToString & _
                " = Selected" & vbCrLf
            Else
                str = str & CheckedListBox1.Items(i).ToString & _
                " = Not Selected" & vbCrLf
            End If
        Next i
        MsgBox(str)
    End Sub

    Private Sub Button2_Click(
ByVal sender As System.Object, _
       
ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox(CheckedListBox1.CheckedItems.Count)
    End Sub

    Private Sub Button3_Click(
ByVal sender As System.Object, _
       
ByVal e As System.EventArgs) Handles Button3.Click
       
For i As Integer = 0 To Me.CheckedListBox1.Items.Count - 1
            CheckedListBox1.SetItemChecked(i, True)
       
Next i
    End Sub

    Private Sub Button4_Click(
ByVal sender As System.Object, _
       
ByVal e As System.EventArgs) Handles Button4.Click
       
For i As Integer = 0 To Me.CheckedListBox1.Items.Count - 1
            CheckedListBox1.SetItemChecked(i, False)
       
Next i
    End Sub

End Class



Runtime
First Load:

Get Item Status:

Count Selected:

Checked All:

Unchecked All:

Click here if you like this article.


Artikel Terkait:
CheckedListBox Event
CheckedListBox Properties
CheckedListBox Control
Common Control Visual Basic .NET
Tutorial Visual Basic.NET

Post a Comment

0 Comments