public class ComboboxItem {
public string Text { get; set; }
public string Value { get; set; }
public override string ToString() { return Text; }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = comboBox1.SelectedIndex;
int selecteVal = (int)comboBox1.SelectedValue;
ComboboxItem selectedCar = (ComboboxItem)comboBox1.SelectedItem;
MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
}
I'm adding them like:
ComboboxItem item = new ComboboxItem();
item.Text = cd.Name;
item.Value = cd.ID;
this.comboBox1.Items.Add(item);
I keep getting a NullReferenceExeption and not sure why. the text seems to show up just fine.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…