473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listbox problem

I have a Listbox binded to as DataSet.
Also I have sub to hadle SelectedIndexCh anged event.
The problem is that every time the dataset is filled the
SelectedIndexCh anged is fired.
How can I prevent this?
Nov 21 '05 #1
4 2202
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim daEmployees As OleDbDataAdapte r

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"

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

conn = New OleDbConnection (strConn)

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

daEmployees.Fil l(ds, "Employees" )

bIgnoreEvent = True

ListBox1.DataSo urce = ds.Tables("Empl oyees")

ListBox1.Displa yMember = "LastName"

End Sub

Private Sub ListBox1_Select edIndexChanged( ByVal sender As Object, ByVal e As
System.EventArg s) Handles ListBox1.Select edIndexChanged

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

Nov 21 '05 #2
Tnanks Ken

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:Ol******** ******@TK2MSFTN GP11.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim daEmployees As OleDbDataAdapte r

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"

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

conn = New OleDbConnection (strConn)

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

daEmployees.Fil l(ds, "Employees" )

bIgnoreEvent = True

ListBox1.DataSo urce = ds.Tables("Empl oyees")

ListBox1.Displa yMember = "LastName"

End Sub

Private Sub ListBox1_Select edIndexChanged( ByVal sender As Object, ByVal e As System.EventArg s) Handles ListBox1.Select edIndexChanged

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

Nov 21 '05 #3
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:Ol******** ******@TK2MSFTN GP11.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 SelectedIndexCh anged. This makes sense to me.

When using a typed dataset in the designer, those properties are set in
InitializeCompo nent before there is any data (hopefully!)... and thus the
event does not fire.

In this same typed dataset scenario, I found that SelectedValueCh anged fired
once when the ValueMember property was set in InitializeCompo nent. That one
does not make sense to me. Especially when adding ListBox1.ValueM ember

I never got a SelectedIndexCh anged 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 SelectedValueCh anged 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("Empl oyees") syntax.

But I want to be clear that it is not the filling of the dataset which is
triggering SelectedIndexCh anged 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
SelectedIndexCh anged.

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******** ******@tk2msftn gp13.phx.gbl...
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:Ol******** ******@TK2MSFTN GP11.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 SelectedIndexCh anged. This makes sense to me.

When using a typed dataset in the designer, those properties are set in
InitializeCompo nent before there is any data (hopefully!)... and thus the
event does not fire.

In this same typed dataset scenario, I found that SelectedValueCh anged fired once when the ValueMember property was set in InitializeCompo nent. That one does not make sense to me. Especially when adding ListBox1.ValueM ember

I never got a SelectedIndexCh anged 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 SelectedValueCh anged 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("Empl oyees") syntax.

But I want to be clear that it is not the filling of the dataset which is
triggering SelectedIndexCh anged 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
SelectedIndexCh anged.

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
540
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 HorizontalScrollbar = true. The text info in listbox already beyond its right edge. How can I see the hidden part info in listbox? here is a part of codes. ...
5
3509
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 double-clicks on one of the variables in listbox A it "moves" it to listbox B. What is going on behind the scenes is that the table that holds all...
2
3670
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 (links the previous tables together so not to have a many-to-many relationship -- has the ContactID and ShowID fields). I have a main form with a...
1
3208
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 the persons, i put each persons inside a ListBox following this:
1
2698
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 also put the same references in a Hashtable so my program can obtain and modify these objects without iterating through entire ListBox looking for...
1
2259
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 button_click function because the only way to find the listbox is: (listbox)e.item.findcontrol What s the solution to this problem?
4
11483
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 select, the first one in the listbox is always removed.(The listitem with an index of 0. Box is set to single selection mode) I've looked at...
8
6369
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 any feasible solution, i need to use a checked listbox which also has same flickering problem on resize. Thanks & Regards
9
3205
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). This listbox has AutoPostback = True. When the user selects an item from this list, the second listbox is populated with more items relevant to this...
15
2350
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 things look better visually for that, I created a listbox under the text box. She enters the number in the text box, clicks another button I added and...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.