【发布时间】:2009-12-05 02:39:15
【问题描述】:
我有一个简单的组合框,其中包含一些值/文本项。我使用 ComboBox.DisplayMember 和 ComboBox.ValueMember 来正确设置值/文本。当我尝试获取该值时,它返回一个空字符串。这是我的代码:
FormLoad 事件:
cbPlayer1.ValueMember = "Value";
cbPlayer1.DisplayMember = "Text";
ComboBox 事件的SelectIndexChanged:
cbPlayer1.Items.Add(new { Value = "3", Text = "This should have a value of 3" });
MessageBox.Show(cbPlayer1.SelectedValue+"");
它返回一个空对话框。我也尝试了 ComboBox.SelectedItem.Value (VS 看到,见图),但它没有编译:
'object' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
我做错了什么?
【问题讨论】: