以编程方式从列表中选择列表框中的项目值

问题描述:

信号问题:我为列表框做了一个搜索功能。如果我搜索一个值;该列表框会清除所有项目,并执行一个items.add函数(它包含文本框中的给定值)。 我想“保存”列表框4中的选定值(列表框5也是选定的项目)。我试图使用setselected函数,但是这个函数不允许使用字符串。是否有解决方法可以保存选定的项目?以编程方式从列表中选择列表框中的项目值

更新:

谢谢,我实现了你的代码片段。

以下是我的代码(正在进行中)。它在listbox4中添加了相同值的多个值。除了选定的值(仅添加一个)之外,它将添加相同的未选定值。除了这个问题,代码的作品。

有没有人有想法?

私人小组TextBox1_TextChanged(发件人为System.Object的,E作为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

你是什么意思的“保存”? *在*调用Clear之前,您可以轻松地将所有选择的项目添加到列表中的列表。 –

+0

我没有添加所有选定的项目到列表框5。但是在调用Clear之后,列表框将再次填充特征,并且必须在列表框4中选择已经选择的特征(列表框5)。不过我可以使用为此选择的一套。但它不起作用。 –

+0

我不确定我关注你。你问*“如何以编程方式在列表框中选择项目”*? –

我不知道所有的列表框如何互相影响,所以这是一个“概念示例“如何添加和选择(选择)从一个列表框到另一个列表框的项目。

让我们假设您有两个列表框:sourcetarget。首先将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)) 
+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