问题描述
我有三个列表框DivisionListBox,SitesListBox和StoresListBox。
- DivisionListBox包含多个商店特许经营权, 文字为部门编号和商店名称,值为 部门编号。 (例如,文本=“ 1-BestBuy”,值1)
- SitesListBox包含商店编号,商店名称和 部门编号为文本,值是商店编号。 (例如文字 = 103-BestBuy [1],值103)
- 在您传递SitesListBox项目之前,StoreListBox为空。 一旦有了物品,我就会使用商店编号的值来执行查询。
DivisionListBox的目的是选择一个专营权,以选择SitesLisBox中属于该专营权的所有项目。
如何在SitesListBox中选择所有项目,其中方括号内的文本与DivisionListBox中的选定值相匹配??
最初,我将DivisionListBox和SitesListBox的值作为部门号,但是SitesListBox中的问题在于,由于多个项目共享相同的值,因此与单个项目的选择混淆了。
当前代码
Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load
DivisionSelection()
End Sub
Private Sub DivisionSelection()
SitesListBox.SelectionMode = ListSelectionMode.Multiple
If DivisionListBox.SelectedValue IsNot nothing Then
For Each item As ListItem In SitesListBox.Items
If item.Value = DivisionListBox.SelectedValue Then
item.Selected = True
End If
Next
End If
End Sub
Protected Sub SiteButton_Click(sender As Object,e As EventArgs) Handles SiteButton.Click
SitesListBox.SelectionMode = ListSelectionMode.Multiple
While SitesListBox.SelectedItem IsNot nothing
StoresListBox.Items.Add(SitesListBox.SelectedItem)
SitesListBox.Items.Remove(SitesListBox.SelectedItem)
End While
SitesListBox.ClearSelection()
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
StoresListBox.ClearSelection()
End Sub
解决方法
我设法用这种方法弄清楚了。
If DivisionListBox.SelectedValue IsNot Nothing Then
Dim selected As String = DivisionListBox.SelectedValue
selected = "[ " + selected + " ]"
Label1.Text = selected
For Each item As ListItem In SitesListBox.Items
If item.Text.Contains(selected) Then
item.Selected = True
End If
Next
End If