|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
listbox's SelectedIndexChanged keep fireI 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 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 ------------------- Show quoteHide quote "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 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". -- Show quoteHide quoteDennis 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 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 Show quoteHide quote "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 Try this:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged If ListBox1.SelectedIndex() = 0 Dim SIC As New ListBox.SelectedIndexCollection(ListBox1) For I = 0 To ListBox1.Items.Count - 1 SIC.Add(I) Next End If End Sub DanyR Show quoteHide quote "martin1" wrote: > 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 |
|||||||||||||||||||||||