views:

66

answers:

2

Hi, 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

+4  A: 

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.

Shoko
This is correct. +1
sindre j
+1 for answering the why
Michael Buen
I'm sorry I didnt get your point. Can you give me a simple example code line?
Nagu
Nagu, not everything can be explained with a line of code.
Henk Holterman
+1  A: 

disable the event prior to binding:

listBox1.SelectedIndexChanged -= listBox1_SelectedIndexChanged;

re-enable after binding:

listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;

Michael Buen