Cal
Cal

Reputation: 867

VB.NET: CheckBoxList - programmatically setting Items as Checked

I pass in comma separated values to this function, and check items in a checkboxlist according to the values. But there are no items checked after the function call.

For example, I pass in a string "1,5,8", hoping the 3 items with value of 1,5,8 in the checkboxlist will get "checked = true" status. But they don't.

Private Sub GetListValuesFromCommaSeparatedValueString(ByRef lst As CheckBoxList, s As String)
    If IsNothing(s) Or s = "" Then
        Exit Sub
    End If

    Dim array = s.Split(",")

    For Each value As String In array
        lst.Items.FindByValue(value).Selected = True
    Next

End Sub

Upvotes: 1

Views: 5251

Answers (2)

Lews Therin
Lews Therin

Reputation: 3777

You'd want the Checked property of CheckBox not Selected.

For Each value As String In array
    lst.Items.FindByValue(value).Checked = True
Next

More info on Checked.

Upvotes: 2

MoreThanChaos
MoreThanChaos

Reputation: 2092

You should use checked property, selected highlights only certain item on list

lst.Items.FindByValue(value).Checked = True

Upvotes: 1

Related Questions