以下のコードを見てください。アプリケーションが起動すると、SelectedIndexChanged が呼び出され、x のタイプは "Example" になります。しかし、アプリが実行され、別のものを選択すると、SelectedIndexChanged は x 型の float の結果を生成します。なぜこれが異なる結果を生むのでしょうか?
{
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; }
}
}