0
votes

Orignal question: I made a search function for a listbox. If i search for a value; the listbox clears all items, and executes a items.add function (which contains the given values from the textbox). I want to "save" the selected values in listbox4 (listbox5 are also the selected items). I tried to use the setselected function, but this function doesn't allow strings. Is there a workaround which saves the selected items?

Update:

Thanks, I implemented your snippet.

Below is my the code (work in progress). It add's multiple values of the same value in listbox4. Besides the selected value (just one added) it add's the same value unselected. Beside this issue the code works.

Does anyone have an idea?

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles 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
2
What do you mean by "save"? You can easily add all selected items to a List<T> before calling Clear. - Bjørn-Roger Kringsjå
I did add all selected Items to the listbox5. But after calling Clear, the listbox will populate again with features, and the already selected features (listbox5) have to be selected in the listbox4. I thoughy I could use a setselected for that purpose. But it doesn't work. - Chris Driessen
I'm not sure I'm following you. Are you asking "how to programmatically select item(s) in a list box"? - Bjørn-Roger Kringsjå
That one way to call it. Yes;) - Chris Driessen
I already tried a setselected while using a count function. This worked fine. But it doesn't use the features which are saved in the other listbox. - Chris Driessen

2 Answers

0
votes

I'm not sure how all your listboxes interact with each other so this is a "conceptual example" of how to add and select (selected) items from one listbox to another.

Lets assume you have two listboxes: source and target. First store all selected items from the source listbox into an array.

Dim selected As Object() = (From item In Me.sourceListBox.SelectedItems Select item).ToArray()

Now, it's safe to to whatever we want with the items in the source listbox.

Me.sourceListBox.Items.Clear()

Next, we'll add all the items to the target listbox.

Me.targetListBox.Items.AddRange(selected)

Finally, we add all items to the SelectedItems collection.

Array.ForEach(selected, Sub(item As Object) Me.targetListBox.SelectedItems.Add(item))
0
votes

This code worked! Thanks for your help.

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