从复选框列表中获取文本

时间:2014-09-22 19:01:48

标签: vb.net winforms checkboxlist

我在check box list中有一个winform

如果选中了复选框列表,那么我希望将值传递给字符串:

For i As Integer = 0 To cbxlstPancakes.Items.Count - 1
    If cbxlstPancakes.GetItemChecked(i) Then

        Dim currentPancake As String = cbxlstPancakes.SelectedItem.ToString

    Else
        'do something if they are not checked.
    End If
Next

1 个答案:

答案 0 :(得分:2)

现在,如果您使用字符串与绑定数据源,我会感到困惑。对于数据源,请尝试其中一个。

如果您只关心CHECKED项目,那就更容易了:

    '=== IF you only care about the checked items (assuming you used a databound control)
    For Each dr As DataRowView In cbxlstPancakes.CheckedItems
        Dim currentPancake As String = dr.Item(0)   '--> TODO:  correct column from your datasource
        MessageBox.Show(currentPancake)
    Next

如果您关心已检查和未检查的项目,您应该能够以这种方式访问​​它们(应该适用于绑定或未绑定):

    '=== IF you care about both checked and unchecked items
    For i As Integer = 0 To cbxlstPancakes.Items.Count - 1
        If cbxlstPancakes.GetItemChecked(i) Then
            MessageBox.Show(cbxlstPancakes.GetItemText(cbxlstPancakes.Items(i)))
        Else
            'Do something if they're not checked
        End If
    Next

我不确定为什么CheckedListBox的实现方式与其他一些控件(使用.Getxxxxx()方法等)略有不同。但这似乎对我有用!