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