C# 如何从KeyValuePair列表中获取颜色?

C# 如何从KeyValuePair列表中获取颜色?,c#,colors,C#,Colors,我有一个按钮:btn,我正在尝试 private List<KeyValuePair<String,Color>> myListKeyValuePair = new List<KeyValuePair<String,Color>> btn.BackColor = Color.(myListKeyValuePair[i].Value); 私有列表myListKeyValuePair=新列表 btn.BackColor=Color.(myList

我有一个按钮:btn,我正在尝试

private List<KeyValuePair<String,Color>> myListKeyValuePair = new List<KeyValuePair<String,Color>>

btn.BackColor =  Color.(myListKeyValuePair[i].Value);
私有列表myListKeyValuePair=新列表
btn.BackColor=Color.(myListKeyValuePair[i].Value);

但是它会显示“identificator expected”。

如果名称/值对中有颜色,只需将back color设置为该值即可

btn.BackColor = myListKeyValuePair[i].Value;

如果您想从键而不是索引中检索颜色(否则您只想使用类型为color的列表)

//示例列表
List myListKeyValuePair=新列表();
添加(新的KeyValuePair(“Truck”,颜色为.Red));
添加(新的KeyValuePair(“Car”,颜色为.Green));
添加(新的KeyValuePair(“Van”,Color.Blue));
//价值检索
颜色myDesiredColor=myListKeyValuePair.Where(item=>item.Key==“Car”).FirstOrDefault().Value;

这是无效的语法。能否显示完整代码/myListKeyValuePair的定义private List myListKeyValuePair=新列表只需使用btn.BackColor=myListKeyValuePair[i].Value;您没有使用数组here@mwwThx,它能工作。我没有注意到几行之后我有了btn.BackColor=另一种颜色。你怎么知道,
myListKeyValuePair
包含了这个名称?我不能肯定这只是一个假设,因为他是如何使用它的。@RyanSearle Thx回答。
    //Sample list
    List<KeyValuePair<string, Color>> myListKeyValuePair = new List<KeyValuePair<string, Color>>();
    myListKeyValuePair.Add(new KeyValuePair<string, Color>("Truck", Color.Red));
    myListKeyValuePair.Add(new KeyValuePair<string, Color>("Car", Color.Green));
    myListKeyValuePair.Add(new KeyValuePair<string, Color>("Van", Color.Blue));

    //Value retrieve
    Color myDesiredColor = myListKeyValuePair.Where(item => item.Key == "Car").FirstOrDefault().Value;