You are getting NullReferenceExeption
because of you are using the cmb.SelectedValue
which is null. the comboBox
doesn't know what is the value of your custom class ComboboxItem
, so either do:
ComboboxItem selectedCar = (ComboboxItem)comboBox2.SelectedItem;
int selecteVal = Convert.ToInt32(selectedCar.Value);
Or better of is use data binding like:
ComboboxItem item1 = new ComboboxItem();
item1.Text = "test";
item1.Value = "123";
ComboboxItem item2 = new ComboboxItem();
item2.Text = "test2";
item2.Value = "456";
List<ComboboxItem> items = new List<ComboboxItem> { item1, item2 };
this.comboBox1.DisplayMember = "Text";
this.comboBox1.ValueMember = "Value";
this.comboBox1.DataSource = items;