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

Home Posts Topics Members FAQ

listbox's SelectedIndexCh anged 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 SelectedIndexCh anged event keep fire and loop
doesn't go to next, finally the program stop at infinite loop.

Sub lstCem_Selected IndexChanged(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles lstCem.Selected IndexChanged
If lstCem.GetSelec ted(0) = True Then
For i = 1 To (lstCem.Items.C ount - 1)
lstCem.SetSelec ted(i, True)
Next
end if
End Sub

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

Try something like this

Private Sub ListBox1_Select edIndexChanged( ByVal sender As Object, ByVal e
As System.EventArg s) Handles ListBox1.Select edIndexChanged
Static boolRunning As Boolean = False

If ListBox1.GetSel ected(0) = True And Not boolRunning Then
boolRunning = True
For i As Integer = 1 To (ListBox1.Items .Count - 1)
ListBox1.SetSel ected(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 SelectedIndexCh anged event keep fire and loop
doesn't go to next, finally the program stop at infinite loop.

Sub lstCem_Selected IndexChanged(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles lstCem.Selected IndexChanged
If lstCem.GetSelec ted(0) = True Then
For i = 1 To (lstCem.Items.C ount - 1)
lstCem.SetSelec ted(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 SelectedIndexCh anged event keep fire and loop
doesn't go to next, finally the program stop at infinite loop.

Sub lstCem_Selected IndexChanged(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles lstCem.Selected IndexChanged
If lstCem.GetSelec ted(0) = True Then
For i = 1 To (lstCem.Items.C ount - 1)
lstCem.SetSelec ted(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_Selected IndexChanged(By Val sender As System.Object, ByVal
e As System.EventArg s) Handles lstCem.Selected IndexChanged

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

RemoveHandler lstCem.Selected IndexChanged, AddressOf
Me.lstCem_Selec tedIndexChanged

lstItem = lstCem.Selected Index
If lstItem = 0 Then
'toggle "all Cems" on
If lstCem.GetSelec ted(0) = True Then
For i = 1 To (lstCem.Items.C ount - 1)
lstCem.SetSelec ted(i, True)
Next
' toggle "all Cems" off
ElseIf lstCem.GetSelec ted(0) = False Then
For i = 1 To (lstCem.Items.C ount - 1)
lstCem.SetSelec ted(i, False)
Next
'boolRunning = False
lstCem.Selected Index = 0 ' put list index back
End If
'boolRunning = False
Else
' toggle "all Cems" off if a cem is deselected
If lstCem.GetSelec ted(lstItem) = False Then

lstCem.SetSelec ted(0, False)
lstCem.Selected Index = lstItem ' put list index back
End If
End If

AddHandler lstCem.Selected IndexChanged, AddressOf
Me.lstCem_Selec tedIndexChanged
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 SelectedIndexCh anged event keep fire and loop
doesn't go to next, finally the program stop at infinite loop.

Sub lstCem_Selected IndexChanged(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles lstCem.Selected IndexChanged
If lstCem.GetSelec ted(0) = True Then
For i = 1 To (lstCem.Items.C ount - 1)
lstCem.SetSelec ted(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
1475
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 because it doesn't do what it's name implies. Is there a way to detect when a user clicks on a DIFFERENT item in the list (without using global...
1
8365
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 I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's...
4
2201
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 prevent this?
18
2002
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 unselected. when listbox2 is selected, 1 and 3 should have no items selected and the same with listbox 3. Sounds simple... should be simple! I have...
0
1097
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) MyListBox..Items.Insert(PostMoveIndex, itemtext) This all works fine. I then decided that I wanted to display a longer text description of
3
7750
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 the form. (It's a Company -> People -> Personal Data relationship) When I bind my datasource to the ListBox, I find that the
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...
7
5587
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 browser the list box scroll position is at the top.
6
4711
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 gets selected. The ListBox waits to fire the event until all selections are complete. Thanks, Mike
0
7605
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
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
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
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
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...
1
5501
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...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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

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.