By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,664 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,664 IT Pros & Developers. It's quick & easy.

setting DataSource fires 'SlectedIndexChanged' event too!

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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!
-----Original Message-----
hi,
i am using a ListBox control on a windows form using VB.Net. when iset the DataSource property of the control to an ArrayList it firesfollowing events in this order:
SelectedIndexChanged
SelectedIndexChanged
DataSourceChanged
SelectedIndexChanged

is there any way to check in SelectedIndexChanged event handler thatif event is fired while setting DataSource property or user hasactually changed the item? SelectedIndex property doesn't work 'causeit is already set to '0'.

BTW, why SelectedIndexChanged is fired 3-times?

Any help...
Thanks
TF
.

Nov 22 '05 #2

P: n/a
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!
-----Original Message-----
hi,
i am using a ListBox control on a windows form using VB.Net. when iset the DataSource property of the control to an ArrayList it firesfollowing events in this order:
SelectedIndexChanged
SelectedIndexChanged
DataSourceChanged
SelectedIndexChanged

is there any way to check in SelectedIndexChanged event handler thatif event is fired while setting DataSource property or user hasactually changed the item? SelectedIndex property doesn't work 'causeit is already set to '0'.

BTW, why SelectedIndexChanged is fired 3-times?

Any help...
Thanks
TF
.

Nov 22 '05 #3

P: n/a
TF
yes, i did same sort of thing but i was wondering if i am missing something.

Thanks for reply
Nov 22 '05 #4

P: n/a
TF
yes, i did same sort of thing but i was wondering if i am missing something.

Thanks for reply
Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.