原始问题:我为列表框创建了搜索功能。如果我搜索一个值;列表框清除所有项目,并执行items.add函数(其中包含文本框中的给定值)。 我想"保存" listbox4中的选定值(listbox5也是所选项)。我试图使用setselected函数,但是这个函数不允许使用字符串。是否有解决方法可以保存所选项目?
更新
谢谢,我实施了你的代码段。
以下是我的代码(正在进行的工作)。它在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
答案 0 :(得分:0)
我不确定你的所有列表框是如何互相交互的,所以这是一个如何从一个列表框添加和选择(选中)项目到另一个列表框的“概念性示例”。
假设您有两个列表框:source
和target
。首先将source
列表框中的所有选定项目存储到数组中。
Dim selected As Object() = (From item In Me.sourceListBox.SelectedItems Select item).ToArray()
现在,使用source
列表框中的项目可以随心所欲。
Me.sourceListBox.Items.Clear()
接下来,我们会将所有项目添加到target
列表框。
Me.targetListBox.Items.AddRange(selected)
最后,我们将所有项目添加到SelectedItems
集合。
Array.ForEach(selected, Sub(item As Object) Me.targetListBox.SelectedItems.Add(item))
答案 1 :(得分:0)
此代码有效!谢谢你的帮助。
Private Sub ListBox4_Click(sender As Object, e As System.EventArgs) Handles ListBox4.Click
Dim selecteditems As String
For Each selecteditems In ListBox4.SelectedItems
ListBox5.Items.Add(selecteditems)
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
''SELECT ALL ITEMS
Dim i As Integer
For i = 0 To Me.ListBox5.Items.Count - 1
ListBox5.SetSelected(i, True)
Next
selected = (From selitem In ListBox5.SelectedItems Select selitem).ToArray()
Array.ForEach(selected, Sub(selitem As Object) ListBox4.SelectedItems.Add(selitem))
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
ListBox4.Items.Clear()
'TOEVOEGEN, alleen toevoegen als deze er nog niet in zit!
For Each item In ListBox3.Items
If item.contains(TextBox1.Text) Then
ListBox4.Items.Add(item)
End If
Next
selected = (From selitem In ListBox5.SelectedItems Select selitem).ToArray()
Array.ForEach(selected, Sub(selitem As Object) ListBox4.SelectedItems.Add(selitem))
End Sub