Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取CheckedListBox项值成员_C# - Fatal编程技术网

C# 获取CheckedListBox项值成员

C# 获取CheckedListBox项值成员,c#,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

我有一个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?

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

checkedListBox1.Items.Insert(0, new {Text = "text1", Value = "value1" });
您可以通过创建一个新类来克服这一问题

class NewType
{
   public string Text;
   public  string Value;
}
那么你的前额会是这样的

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

值成员的可能副本适用于来自绑定源等的所有项目值。。那么为什么要访问foreach内部的r请指定并同意@Proputix duplicate。我还使用此代码label1.Text=checkedListBox1.ValueMember在ValueMember的标签中赋值;