Vb.net 从复选框列表中获取文本

Vb.net 从复选框列表中获取文本,vb.net,winforms,checkboxlist,Vb.net,Winforms,Checkboxlist,我在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 somethi

我在
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

现在我搞不清楚您是使用字符串还是绑定数据源。对于数据源,请尝试其中一种

如果您只关心选中的项目,则更容易:

    '=== 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
如果您同时关心选中项和未选中项,则应该能够以这种方式访问它们(应适用于绑定或未绑定项):


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

cbxlstPancakes.Items(i).ToString
当它返回时,我已经尝试过了:System.Data.datarowview您没有记录列表中除了字符串之外还有其他内容。你必须将它投射到你正在使用的对象上。对不起,我是个笨蛋,但我只使用了一个字符串。复选框列表中有一个DisplayMember和一个ValueMember。您知道用户改变了主意,并且if影响了我的工作模式。谢谢,这就是我想要的。作为将来的参考,如果我想为复选框分配一个ID。如何获取它们的值?这与字符串值无关。但这在下面奏效了>再次感谢CheckedListBox1.CheckedItems Dim currentPancake As String=dr'-->TODO:数据源消息框中的正确列。Show(currentPancake.ToString)
    '=== 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