"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:Ol**************@TK2MSFTNGP11.phx.gbl...
Hi,
You need to add a boolean variable to you form. Use it to prevent
the code from running after the first selected index changed event.
I might clarify this statement by saying that you may not need to do this if
you are using typed datasets. In tracing through your code, it seems that
it is the setting of the DataSource and DisplayMember properties which
trigger SelectedIndexChanged. This makes sense to me.
When using a typed dataset in the designer, those properties are set in
InitializeComponent before there is any data (hopefully!)... and thus the
event does not fire.
In this same typed dataset scenario, I found that SelectedValueChanged fired
once when the ValueMember property was set in InitializeComponent. That one
does not make sense to me. Especially when adding ListBox1.ValueMember
I never got a SelectedIndexChanged call. A side effect of this is that
after the form and data are loaded, the ListBox is sitting on the first row,
and the event never fired. This speaks strongly in favor of your
bIgnoreEvent technique, if one wishes to know that the first list item was
indeed selected.
I found that SelectedValueChanged was a bit worse... It was called 2 or 3
times by setting the DataSource property, depending on whether the dataset
was typed or not. Figure that one out! Maybe it had to do with the late
binding of the ds.Tables("Employees") syntax.
But I want to be clear that it is not the filling of the dataset which is
triggering SelectedIndexChanged multiple times - at least not in this
particular case. It is the setting of the properties. And if they are set
before the dataset is filled, it does not appear to be an issue for
SelectedIndexChanged.
Best Regards,
Andy