none
Loop through a listbox and remove item ? RRS feed

  • Question

  • I want to loop through a Listbox and remove item based on a textbox text.

     Dim i As Integer
            For Each item In ListBox1.Items
                For i = 0 To ListBox1.Items.Count - 1
                    If ListBox1.Items(i).ToString.Contains(TextBox2.Text) Then
                        ListBox1.Items.Remove(i)
                    End If
                Next
            Next

    Thursday, November 8, 2018 11:40 AM

Answers

  • Try this

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = ListBox1.Items.Cast(Of String).
                    Where(Function(item) Not item.Contains(TextBox1.Text)).ToArray()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(result)
        End Sub
    End Class

    Or

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = ListBox1.Items.Cast(Of String).
                    Where(Function(item) item.IndexOf(
                        TextBox1.Text, StringComparison.InvariantCultureIgnoreCase) = -1).ToArray()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(result)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, November 8, 2018 11:57 AM
    Moderator

All replies

  • Hi

    Set up your loop from last to first as doing it the way you show will fail with an exception as the List 'has been modified' and changed indexes etc.


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, November 8, 2018 11:49 AM
    Thursday, November 8, 2018 11:48 AM
  • Try this

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = ListBox1.Items.Cast(Of String).
                    Where(Function(item) Not item.Contains(TextBox1.Text)).ToArray()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(result)
        End Sub
    End Class

    Or

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = ListBox1.Items.Cast(Of String).
                    Where(Function(item) item.IndexOf(
                        TextBox1.Text, StringComparison.InvariantCultureIgnoreCase) = -1).ToArray()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(result)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, November 8, 2018 11:57 AM
    Moderator
  • Try this

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = ListBox1.Items.Cast(Of String).
                    Where(Function(item) Not item.Contains(TextBox1.Text)).ToArray()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(result)
        End Sub
    End Class

    Or

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = ListBox1.Items.Cast(Of String).
                    Where(Function(item) item.IndexOf(
                        TextBox1.Text, StringComparison.InvariantCultureIgnoreCase) = -1).ToArray()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(result)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thank you

    Thursday, November 8, 2018 2:42 PM