Nagu
Nagu

Reputation: 5114

Listbox selected event firing automatically?

I've a winform and I'm trying to bind some elements at page load method. After that

listBox1_SelectedIndexChanged event fires automatically. Why it is happening?

Thanks in advance, Nagu

Upvotes: 0

Views: 783

Answers (2)

Michael Buen
Michael Buen

Reputation: 39443

disable the event prior to binding:

listBox1.SelectedIndexChanged -= listBox1_SelectedIndexChanged;

re-enable after binding:

listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;

Upvotes: 1

Tola Odejayi
Tola Odejayi

Reputation: 3059

I assume that this is because your list box begins life with no items in it (so its SelectedIndex property is -1). As soon as it's populated, its SelectedIndex property changes to 0 (to select the first item in the now populated list box) and then SelectedIndexChanged event is now fired.

Upvotes: 4

Related Questions