Question : capture value of checked items in checklistbox

Hello,

I'm want to capture the value of the checked items in a checklistbox in vb .NET (winforms).

My check list box data looks like this:
Value;   Display
23          Elmora
78          Sussex
89          Mountainside

If Elmora and Sussex are checked I want to capture 23, and 78.

Thank you in advance.

My code is attached
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
Public Sub FillChkListBox(ByVal lb As CheckedListBox, ByVal sql As String, ByVal ParameterName As String, ByVal ParameterValue As String)
        Dim connetionString As String
        Dim connection As SqlConnection
        Dim command As SqlCommand

        Dim dt As DataTable

        Dim adapter As New SqlDataAdapter
        '        Dim ds As New DataSet
        Dim i As Int32

        connetionString = myCN

        connection = New SqlConnection(connetionString)

        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.AddWithValue(ParameterName.ToString, ParameterValue.ToString)

            dt = New DataTable
            dt.Load(command.ExecuteReader)

            adapter.Dispose()
            command.Dispose()
            connection.Close()



            lb.DataSource = dt
            lb.DisplayMember = dt.Columns(1).ColumnName
            lb.ValueMember = dt.Columns(0).ColumnName




        Catch ex As Exception


        End Try

    End Sub


    Private Sub lblAddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAddToList.Click



        For Each x In Me.chkBoxAvailable.CheckedIndices
''  I want to capture the value not the text of each checked item

            MsgBox(Me.chkBoxAvailable.Text.ToString)
            MsgBox(Me.chkBoxAvailable.SelectedValue.ToString)

        Next




    End Sub

Answer : capture value of checked items in checklistbox

Try something like this out:
1:
2:
3:
4:
5:
    Private Sub lblAddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAddToList.Click
        For Each drv As DataRowView In lb.CheckedItems
            Debug.Print(drv.Item(lb.DisplayMember) & " --> " & drv.Item(lb.ValueMember))
        Next
    End Sub
Random Solutions  
 
programming4us programming4us