setting DataSource fires 'SlectedIndexChanged' event too!

  • Thread starter Thread starter TF
  • Start date Start date
T

TF

hi,
i am using a ListBox control on a windows form using VB.Net. when i
set the DataSource property of the control to an ArrayList it fires
following events in this order:
SelectedIndexChanged
SelectedIndexChanged
DataSourceChanged
SelectedIndexChanged

is there any way to check in SelectedIndexChanged event handler that
if event is fired while setting DataSource property or user has
actually changed the item? SelectedIndex property doesn't work 'cause
it is already set to '0'.

BTW, why SelectedIndexChanged is fired 3-times?

Any help...
Thanks
TF
 
I have experienced this same problem in the past and have
resolved the issue by using a no-so-elegant but workable
solution using a module level boolean variable.

For example, declare a variable in your form as such...

Private mboolSettingDataSource As Boolean = False

Then prior to setting the DataSource property set the
variable to True then after setting the DataSource
property set the variable back to False.

Then at the begining of the SelectedIndexChanged event
place a code snipet like...

If mboolSettingDataSource Then
Exit Sub
End If

FWIW: I believe the reason the SelectedIndexChanged event
fires multiple times is because internally the DataSource
property actually changes the SelectedIndex porperty
(probably once for each element in the array).

I hope this helps!
 
yes, i did same sort of thing but i was wondering if i am missing something.

Thanks for reply
 
Back
Top