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

Listbox problem

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


P: n/a
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
-----------------------
"Nikolay Petrov" <jo******@mail.bg> wrote in message
news:OA**************@TK2MSFTNGP10.phx.gbl...
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?

Nov 21 '05 #2

P: n/a
Tnanks Ken

"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.

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
-----------------------
"Nikolay Petrov" <jo******@mail.bg> wrote in message
news:OA**************@TK2MSFTNGP10.phx.gbl...
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?

Nov 21 '05 #3

P: n/a
"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
Nov 21 '05 #4

P: n/a
So Andy, any idea how to fix the problem?
"Andy Becker" <x@x.com> wrote in message
news:eu**************@tk2msftngp13.phx.gbl...
"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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.