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