问题
The DivisionListBox is used to select values in the SitesListbox as follows: When you select an item in the DivisionListBox it will select all the items on SitesListbox that match the same value.
Once the SiteListbox values are selected I use the SiteButton to move all selected values to the empty StoreslistBox successfully.
My problem happens when I manually select multiple items on either SiteListbox or StoreslistBox and press SiteButton or StoreButton to move items between Listboxes. It either moves one item at a time or some of the selected items but it never moves all of the selected items (which can be track by the labels).
What is causing this problem and what could be a possible solution for this?
My code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DivisionSelection()
End Sub
Private Sub DivisionSelection()
If DivisionListBox.SelectedValue IsNot Nothing Then
SitesListBox.SelectionMode = ListSelectionMode.Multiple
For Each item As ListItem In SitesListBox.Items
If item.Value = DivisionListBox.SelectedValue Then
item.Selected = True
End If
Next
End If
Dim x As Integer = 0
Dim Siteitem As ListItem
For Each Siteitem In SitesListBox.Items
If Siteitem.Selected Then
x += 1
Label1.Text = x
End If
Next
End Sub
Protected Sub SiteButton_Click(sender As Object, e As EventArgs) Handles SiteButton.Click
StoresListBox.SelectionMode = ListSelectionMode.Multiple
While SitesListBox.SelectedItem IsNot Nothing
StoresListBox.Items.Add(SitesListBox.SelectedItem)
SitesListBox.Items.Remove(SitesListBox.SelectedItem)
End While
'counts the selected items
Dim x As Integer = 0
Dim Siteitem As ListItem
For Each Siteitem In SitesListBox.Items
If Siteitem.Selected Then
x += 1
Label1.Text = x
End If
Next
End Sub
Protected Sub StoreButton_Click(sender As Object, e As EventArgs) Handles StoreButton.Click
StoresListBox.SelectionMode = ListSelectionMode.Multiple
While StoresListBox.SelectedItem IsNot Nothing
SitesListBox.Items.Add(StoresListBox.SelectedItem)
StoresListBox.Items.Remove(StoresListBox.SelectedItem)
End While
'counts the selected items
Dim x As Integer = 0
Dim Storeitem As ListItem
For Each Storeitem In SitesListBox.Items
If Storeitem.Selected Then
x += 1
Label2.Text = x
End If
Next
End Sub
回答1:
I added the selected items to a list; then looped through the list to do the adding and removing. When a collection or indexes are messed with inside a loop it can have unexpected results.
Protected Sub Button1_Click(ByVal sender As Object, e As System.EventArgs) Handles Button1.Click
Dim lstItems As New List(Of ListItem)
For Each item As ListItem In ListBox1.Items
If item.Selected Then
lstItems.Add(item)
End If
Next
'It wasn't clear if you wanted the items to remain selected after moving.
ListBox1.ClearSelection()
Debug.Print(lstItems.Count.ToString)
For Each item As ListItem In lstItems
ListBox2.Items.Add(item)
ListBox1.Items.Remove(item)
Next
End Sub
来源:https://stackoverflow.com/questions/64395042/asp-netvb-listboxes-are-not-moving-all-the-selected-items-to-other-listboxes