【问题标题】:List for both checkedlist box and combobox选中列表框和组合框的列表
【发布时间】:2014-04-14 13:32:55
【问题描述】:

我想将选中列表框和组合框的所有选定项添加到列表中,并在 c# 中的 for each 循环中使用该列表

我试过了

List<String> list=new List<String>();

if (rbtnMultipleScenario.Checked == true)
{
    foreach ( CheckedListBox str in clbScenario.SelectedItems)
    {                    
         lstitems.Add(str);
    }                  
}

通过使用字符串,我无法添加 Checkedlistbox 的所有选定项。

我必须使用哪种类型的列表?

【问题讨论】:

  • 因为没有以CheckedListBox为参数的List&lt;T&gt;.Add方法的重载。您为什么不尝试使用.Text.ValueMember(存在哪一个)属性呢?
  • 实际上,借助“T”参数,您可以毫无问题地使用 List

标签: c# list combobox checkedlistbox


【解决方案1】:
List<string> list=new List<string>();

if (rbtnMultipleScenario.Checked == true)
{
    foreach ( string str in clbScenario.SelectedItems)
    {                    
         lstitems.Add(str);
    }                  
}

这假设 SelectedItems 包含一个字符串集合(你的例外情况就是这样)

【讨论】:

  • 异常-----无法将 System.String 类型的对象转换为 System.Windows.Forms.CheckedListBox 类型
  • 您使用哪些对象来构建您的选择列表框集合(在 clbScenario 中)? clbScenario.SelectedItems 是对象的集合 - 如果您已将字符串添加到其中,那么您将获得字符串。我会更新我的答案
  • 如何在foreach循环中添加checkedlistbox的所有选中项?
  • 对于字符串,它没有在 .SelectedItems 中获取所有选定的项目
  • 您能否展示将项目添加到 clbScenario 以在 UI 中显示的代码?我想知道你是不是在混合类型。
猜你喜欢
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
相关资源
最近更新 更多