【发布时间】:2013-04-05 19:42:22
【问题描述】:
请看下面的代码。当应用程序启动时 SelectedIndexChanged 被调用并且 x 是“示例”类型。但是当应用程序运行并且您选择不同的东西时,SelectedIndexChanged 会产生一个结果 x 类型的浮点数。为什么这会产生不同的结果?
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var example = new List<Example>();
example.Add(new Example("A", 100f));
example.Add(new Example("B", 200f));
example.Add(new Example("C", 400f));
this.comboBox1.DataSource = example;
this.comboBox1.DisplayMember = "Description";
this.comboBox1.ValueMember = "Value";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var x = this.comboBox1.SelectedValue;
}
}
public class Example
{
public Example(string desc, float val)
{
this.Description = desc;
this.Value = val;
}
public string Description { get; set; }
public float Value { get; set; }
}
}
【问题讨论】:
-
在大多数情况下,您可能想回复
SelectionChangeCommitted,即当用户交互实际参与组合框时。
标签: c# winforms events combobox