获取CheckedListBox项ValueMember

时间:2018-08-12 17:16:55

标签: c#

我有一个CheckedListBox,其中充满了代码:

checkedListBox1.DisplayMember = "Text";
checkedListBox1.ValueMember = "Value";

checkedListBox1.Items.Insert(0, new {Text = "text1", Value = "value1" });
checkedListBox1.Items.Insert(0, new {Text = "text2", Value = "value2" });
checkedListBox1.Items.Insert(0, new {Text = "text3", Value = "value3" });

foreach (var item in checkedListBox1.Items)
{
    // ?
}

如何获取ValueMember

1 个答案:

答案 0 :(得分:0)

这是因为您要在此处创建匿名类型

checkedListBox1.Items.Insert(0, new {Text = "text1", Value = "value1" });

您可以通过创建一个新的类

来解决此问题
class NewType
{
   public string Text;
   public  string Value;
}

然后您的Foreach会像这样

foreach (NewType item in checkedListBox1.Items)
        {
            item.value
        }