【发布时间】:2014-12-24 11:22:47
【问题描述】:
原始问题:我为列表框制作了搜索功能。如果我搜索一个值;列表框清除所有项目,并执行 items.add 函数(其中包含来自文本框的给定值)。 我想在 listbox4 中“保存”选定的值(listbox5 也是选定的项目)。我尝试使用 setselected 函数,但该函数不允许字符串。是否有保存所选项目的解决方法?
更新:
谢谢,我实现了你的 sn-p。
以下是我的代码(正在进行中)。它在 listbox4 中添加相同值的多个值。除了选定的值(仅添加了一个)之外,它添加了相同的未选择值。除了这个问题,代码可以工作。
有人有想法吗?
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) 处理 TextBox1.TextChanged
Dim selected As Object()
selected = (From selitem In ListBox5.SelectedItems Select selitem).ToArray()
ListBox4.Items.Clear()
For Each item In ListBox3.Items
If item.contains(TextBox1.Text) Then
ListBox4.Items.Add(item)
End If
Next
For Each item In ListBox5.Items
If item.contains(TextBox1.Text) Then
ListBox4.Items.AddRange(selected)
Array.ForEach(selected, Sub(selitem As Object) ListBox4.SelectedItems.Add(selitem))
End If
Next
End Sub
Private Sub ListBox4_Click(sender As Object, e As System.EventArgs) Handles ListBox4.Click
Dim additems As String
For Each additems In ListBox4.SelectedItems
ListBox5.Items.Add(additems)
Next
''REMOVE DUPLICATES
Dim List As New ArrayList
For Each item1 As String In ListBox5.Items
If Not List.Contains(item1) Then
List.Add(item1)
End If
Next
ListBox5.Items.Clear()
For Each item2 As String In List
ListBox5.Items.Add(item2)
Next
Dim i As Integer
For i = 0 To Me.ListBox5.Items.Count - 1
Me.ListBox5.SetSelected(i, True)
Next
End Sub
【问题讨论】:
-
“保存”是什么意思?您可以轻松地将所有选定的项目添加到
List<T>之前调用Clear。 -
我确实将所有选定的项目添加到列表框5。但是在调用 Clear 之后,列表框将再次填充特征,并且必须在列表框 4 中选择已经选择的特征(列表框 5)。我以为我可以为此目的使用 setselected 。但它不起作用。
-
我不确定我是否在关注你。您是在问“如何以编程方式选择列表框中的项目”?
-
这是一种称呼方式。是的;)
-
我已经在使用计数功能时尝试了 setselected。这工作得很好。但它不使用保存在其他列表框中的功能。
标签: vb.net listbox selected items