By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

listbox's SelectedIndexChanged keep fire

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.