-1
comboBox.Items.AddRange(Enumerable.Range(18, 38).Cast<object>().ToArray());
comboBox.SelectedIndex = 0;
Console.WriteLine(comboBox.SelectedValue + "test");

I get just "test" without the "18" printed. I also don't clearly understand what's the difference between SelectedValue and SelectedItem, even if I read the guide.

Bersekz
  • 29
  • 6
  • Does [this question](https://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath) not help? `SelectedValue` relies on `SelectedValuePath`. It seems you want `SelectedItem` here. – Charles Mager Jan 08 '18 at 17:26
  • 2
    The WPF combobox's Items collection doesn't have an AddRange() method. Is this really winforms, not WPF? In winforms, `ComboBox.Items` has an `AddRange(object[] items)` method. – 15ee8f99-57ff-4f92-890c-b56153 Jan 08 '18 at 17:28

1 Answers1

2

You're populating your combo box with integers, which have no value property. They are the value. So what you want is SelectedItem, which gives you the whole object the user selected.