listbox's SelectedIndexChanged keep fire

G

Guest

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
 
G

Guest

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
 
G

Guest

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".
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top