【问题标题】:Get the text from a check box list从复选框列表中获取文本
【发布时间】:2014-09-22 19:01:47
【问题描述】:

我在winform 中有一个check box list。

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

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

【问题讨论】:

  • cbxlstPancakes.Items(i).ToString
  • 我已经尝试过了,因为它返回:System.Data.DataRowView
  • 您没有记录列表中除了字符串之外还有其他内容。您必须将其强制转换为您正在使用的对象。
  • 对不起,我是个笨蛋,但我只使用了一个字符串。复选框列表确实有一个 DisplayMember 和一个 ValueMember。

标签: vb.net winforms checkboxlist


【解决方案1】:

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

如果你只关心 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() 方法等)略有不同。但这似乎对我有用!

【讨论】:

  • 你知道用户改变了主意,如果影响我的工作模式。谢谢这是我想要的。以供将来参考,如果我想为复选框分配一个 ID。我如何获得它们的价值?
  • 这不适用于字符串值。但这在下面有效>再次感谢For Each dr As String In CheckedListBox1.CheckedItems Dim currentPancake As String = dr '--> TODO: correct column from your datasource MessageBox.Show(currentPancake.ToString)
猜你喜欢
  • 1970-01-01
  • 2015-11-03
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多