473,396 Members | 1,784 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,396 software developers and data experts.

listbox's SelectedIndexChanged keep fire

Hi, All,

I want user select first item (called All) in listbox, then all other items
are selected by SetSelected method, but in loop (see code below) whenever
going to SetSelected(), the SelectedIndexChanged event keep fire and loop
doesn't go to next, finally the program stop at infinite loop.

Sub lstCem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lstCem.SelectedIndexChanged
If lstCem.GetSelected(0) = True Then
For i = 1 To (lstCem.Items.Count - 1)
lstCem.SetSelected(i, True)
Next
end if
End Sub

can anyone help thisout?
Thanks,
Marin
Nov 27 '06 #1
3 2809
Hi,

Try something like this

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Static boolRunning As Boolean = False

If ListBox1.GetSelected(0) = True And Not boolRunning Then
boolRunning = True
For i As Integer = 1 To (ListBox1.Items.Count - 1)
ListBox1.SetSelected(i, True)
Next
boolRunning = False
End If

Ken
-------------------

"martin1" wrote:
Hi, All,

I want user select first item (called All) in listbox, then all other items
are selected by SetSelected method, but in loop (see code below) whenever
going to SetSelected(), the SelectedIndexChanged event keep fire and loop
doesn't go to next, finally the program stop at infinite loop.

Sub lstCem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lstCem.SelectedIndexChanged
If lstCem.GetSelected(0) = True Then
For i = 1 To (lstCem.Items.Count - 1)
lstCem.SetSelected(i, True)
Next
end if
End Sub

can anyone help thisout?
Thanks,
Marin
Nov 27 '06 #2
Using a static variable works as does a global. However, for general use,
you might try "RemoveHandler", do your thing, then "AddHandler". This will
prevent the event from firing while you "do your thing".
--
Dennis in Houston
"martin1" wrote:
Hi, All,

I want user select first item (called All) in listbox, then all other items
are selected by SetSelected method, but in loop (see code below) whenever
going to SetSelected(), the SelectedIndexChanged event keep fire and loop
doesn't go to next, finally the program stop at infinite loop.

Sub lstCem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lstCem.SelectedIndexChanged
If lstCem.GetSelected(0) = True Then
For i = 1 To (lstCem.Items.Count - 1)
lstCem.SetSelected(i, True)
Next
end if
End Sub

can anyone help thisout?
Thanks,
Marin
Nov 28 '06 #3
Hi, Dannis,

I tried addhander and one task (see below code) called toggle "all cems on"
works, but another 2 tasks: 1) ' toggle "all Cems" off , and 2) ' toggle "all
Cems" off if a cem is deselected, are not work. the task 1) is is if user
deselect first item called All Cems, then all other cem deselect, the task
2) is if any one but first one deselect, then the first one (called all cems)
is deselect. I think selectIndex logic may wrong but don't know how to fix
that. Any more advice? Thanks for your time.
Private Sub lstCem_SelectedIndexChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles lstCem.SelectedIndexChanged

Dim lstItem As Short
Dim i As Short
Static boolRunning As Boolean = False

RemoveHandler lstCem.SelectedIndexChanged, AddressOf
Me.lstCem_SelectedIndexChanged

lstItem = lstCem.SelectedIndex
If lstItem = 0 Then
'toggle "all Cems" on
If lstCem.GetSelected(0) = True Then
For i = 1 To (lstCem.Items.Count - 1)
lstCem.SetSelected(i, True)
Next
' toggle "all Cems" off
ElseIf lstCem.GetSelected(0) = False Then
For i = 1 To (lstCem.Items.Count - 1)
lstCem.SetSelected(i, False)
Next
'boolRunning = False
lstCem.SelectedIndex = 0 ' put list index back
End If
'boolRunning = False
Else
' toggle "all Cems" off if a cem is deselected
If lstCem.GetSelected(lstItem) = False Then

lstCem.SetSelected(0, False)
lstCem.SelectedIndex = lstItem ' put list index back
End If
End If

AddHandler lstCem.SelectedIndexChanged, AddressOf
Me.lstCem_SelectedIndexChanged
End Sub

"Dennis" wrote:
Using a static variable works as does a global. However, for general use,
you might try "RemoveHandler", do your thing, then "AddHandler". This will
prevent the event from firing while you "do your thing".
--
Dennis in Houston
"martin1" wrote:
Hi, All,

I want user select first item (called All) in listbox, then all other items
are selected by SetSelected method, but in loop (see code below) whenever
going to SetSelected(), the SelectedIndexChanged event keep fire and loop
doesn't go to next, finally the program stop at infinite loop.

Sub lstCem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lstCem.SelectedIndexChanged
If lstCem.GetSelected(0) = True Then
For i = 1 To (lstCem.Items.Count - 1)
lstCem.SetSelected(i, True)
Next
end if
End Sub

can anyone help thisout?
Thanks,
Marin
Nov 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: PeterZ | last post by:
Hi, Is SelectedIndexChanged supposed to fire even when the user clicks on the same item in a ListBox? If that's the case then they should rename the event to OnClicked or something like that...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
4
by: Nikolay Petrov | last post by:
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...
18
by: Dave Sauny | last post by:
Ok, its a friday, I'm at work and I cant get this to work: I have 3 listboxes on one tab control page. when i select an item in listbox1 i want whatever is selected on the other 2 listboxes...
0
by: John Dann | last post by:
I had a listbox that allows reordering of listed items using the mouse to drag and drop. In the DragDrop event handler, the key instructions are: MyListBox..Items.RemoveAt(PreMoveIndex)...
3
by: Alec MacLean | last post by:
Hi everyone, I have a ComboBox that when changed, calls a method to change the content of a ListBox. I'm also using the Listbox's SelectedIndexChanged event to change other control values on...
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)....
7
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
6
by: Mike | last post by:
Is there a way to get a ListView control to fire its SelectedIndexChanged event like the ListBox control? When the user selects multiple items at once, the ListView fires the event as each item...
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: 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...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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,...

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.