473,722 Members | 2,430 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 2212
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. ListBox.ValueMember = XX ListBox.DisplayMember = XXXX
5
3528
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 these variables has a yes/no field call Print. When the user double clicks on the value it fires a...
2
3676
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 couple of listboxes which are used for querying the CONTACTS table. The results populate bound fields...
1
3212
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
2706
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 what I want. The problem is though, when I update these objects the changes are not being reflected...
1
2265
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
11495
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 multiple examples and they all do it this way. What's wrong? (variables are also being set to the values...
8
6382
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
3243
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 selection. I am using an SQLDataSource web control for this. These items are headers. I want, when...
15
2364
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 the number is stored in the list box. Then my plan was to grab all those numbers from the list box...
0
8863
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9238
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
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
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.