Question

How do I retrieve values from the Check box list control in visual studio using visual basic? Currently I'm using "Reason.Text", but I need to retrieve all the variables that are checked. This is a picture of the Checkbox list.

Was it helpful?

Solution 2

Take a look at this simple sample :

enter image description here

Here the button code :

 Protected Sub btnGetValues_Click(sender As Object, e As EventArgs) Handles btnGetValues.Click

    Dim Sb As New System.Text.StringBuilder


    'to get al values using linq reaons.items as Listitem which expose selected property and you may filter on this
    'property too as per my example
    Dim X = (From a As ListItem In reason.Items Where a.Selected = True Select a.Value).ToList
    For Each el In X
        Sb.Append(el.ToString & "<br/>")
    Next
    lit1.Text = Sb.ToString
    Sb.Clear()


    'to get anonymous type with multiples params you may use this:
    Dim k = (From a As ListItem In reason.Items Where a.Selected = True Select New With {.Checked = a.Selected, .TextValue = a.Text, .IDValue = a.Value}).ToList

    For Each el In k
        Sb.Append(String.Format("IsSelected=<b>{0}</b>, TextNameValue=<b>{1}</b>, IntegerValue=<b>{2}</b><br/>", el.Checked, el.TextValue, el.IDValue))
    Next
    lit2.Text = Sb.ToString
    Sb.Clear()
    Sb = Nothing


End Sub

if it is exactly what you0re searching for mark as answered

OTHER TIPS

did you mean some thing like thes

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     Dim st As String = String.Empty
     For Each item As Object In Me.CheckedListBox1.CheckedItems
          st += item.ToString & vbCrLf
     Next
     MessageBox.Show(st)
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top