【问题标题】:Retrieving CheckedListBox Selected Items as Dictionary检索 CheckedListBox 选定项作为字典
【发布时间】:2015-08-04 20:51:39
【问题描述】:

我的表单上有一个选中列表框,我使用字典填充它。当我填充该框时它工作正常,但是当我尝试检索所选对象时,我无法弄清楚如何将它作为字典。

我的代码来填充它:

reader = widgetSelection.ExecuteReader();
while (reader.Read())
{
 widgets.Add(reader.GetInt32(0), reader.GetString(reader.GetOrdinal("name")));
}

foreach (var widget in widgets)
{
    chbWidgets.Items.Add(widget);
}

它完美地填充,但任何尝试执行 foreach 或任何事情都只会作为对象返回,我无法弄清楚投射项目的正确方法。任何帮助都会很棒。

【问题讨论】:

    标签: c# dictionary checkedlistbox


    【解决方案1】:

    如果您的示例中的widgetsDictionary<int, string>,则chbWidgets.Items.Add(widget) 正在将KeyValuePair<int, string> 添加到Items 集合中。要获取选中的项目,您可能可以使用chbWidgets.CheckedItems.Cast<KeyValuePair<int, string>>()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      相关资源
      最近更新 更多