Stuart Grassie
Stuart Grassie

Reputation: 3073

Setting the SelectedIndex of a ComboBox throws ArgumentOutOfRangeException

Given the following code, how is that I receive an ArguementOutOfRangeException?

if (comboBox1.Items.Count > 0)
{
    comboBox1.SelectedIndex = 0;
}

This code is in my forms Load event. I know the combo box has items in it, but yet I cannot set the selected index. MSDN says that the ArguementOutOfRangeException is thrown if the specified index is less than or equal to -2, or if it is greater than or equal to the number of items in the combobox.

The quick test app I just wrote doesn't exhibit the same problem - what could be going on?

Upvotes: 1

Views: 2495

Answers (1)

Stuart Grassie
Stuart Grassie

Reputation: 3073

Aren't I quite the idiot. The selected index changed event was firing, and in that code a different combobox had it's selectedIndex set to 0 - however with certain data it wasn't being populated, and there was no basic error checking before setting the selectedIndex.

Sigh.

Upvotes: 2

Related Questions