Listbox problem

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
N

Nikolay Petrov

I have a Listbox binded to as DataSet.
Also I have sub to hadle SelectedIndexChanged event.
The problem is that every time the dataset is filled the
SelectedIndexChanged is fired.
How can I prevent this?
 
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.

Dim ds As New DataSet

Dim bIgnoreEvent As Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim daEmployees As OleDbDataAdapter

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

daEmployees = New OleDbDataAdapter("Select * From Employees Order by
LastName, FirstName", conn)

daEmployees.Fill(ds, "Employees")

bIgnoreEvent = True

ListBox1.DataSource = ds.Tables("Employees")

ListBox1.DisplayMember = "LastName"



End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexChanged

If bIgnoreEvent Then

bIgnoreEvent = False

Return

End If

'your code goes here

End Sub



Ken
-----------------------
I have a Listbox binded to as DataSet.
Also I have sub to hadle SelectedIndexChanged event.
The problem is that every time the dataset is filled the
SelectedIndexChanged is fired.
How can I prevent this?
 
Ken Tucker said:
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
 
So Andy, any idea how to fix the problem?


Andy Becker said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top