473,394 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Listbox problem

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
4 2176
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Brian | last post by:
Hi, All, I'm using MS.net 2003 and using a windows.forms.listbox control for my window application but I don't know why the Horizontal Scrollbar could NOT be shown even if I set...
5
by: Bill | last post by:
I have have two list boxes. One is a listing of all possible variables. We'll call this listbox A. The other is a listing of all the selected variables. We'll call this listbox B. If a person...
2
by: Simon P | last post by:
Hello group, I'm in desperate need of help. Here goes : I have the following tables : CONTACTS (ContactID, FirstName, LastName, Company, etc.), SHOWS (ShowID, ShowDescription) and SHOWDETAILS...
1
by: Josema | last post by:
Hi to all, I have a class (persons) that derives from collection base: and another class (person) with this properties: -ID -Name When i have complete filled the object Persons with all...
1
by: MrNobody | last post by:
Hi, I'm doing something where I add custom objects to a ListBox which have aToString() method overriden so it displays what I want. When adding instances of these custom objects to the ListBox I...
1
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
8
by: nirdeshonline | last post by:
Hi, I have added a simple listbox in windows form under c# 2.0. It contains a collection of approx 10 strings as list items. Now when i resize the form whole listbox flickers. Please tell me...
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
15
by: Doogie | last post by:
I have a .net app that a user currently enters a number in a text box, hits a button and a data call is executed. She wants the ability to enter in multiple numbers (up to 100). So to make...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.