locked
Remove multiple items from listbox RRS feed

  • Question

  • Suppose I have 5 items in my listbox

    Cheeze Product A

    Product B Cheese

    Product Cheeze C

    Ice Cream A

    Ice Cream B

    Also I have a button, I want that when i click that button, my listbox should remove those items completely that contains "Cheeze" and after removing those items my listbox should now look like below

    Ice Cream A

    Ice Cream B

    Can someone help me out

    Thanks

    Friday, July 19, 2013 10:20 AM

Answers

  • Sid,

    If you want to do it through iteration, then do it in reverse order:

    For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

    Then in the loop, test to see if ListBox1.Items(i).Contains("term to search for") and if so, use .RemoveAt(i).

    The reason why you can't do it in forward order is that you're changing the count. It normally would move toward that count (minus one), but you just changed that count when you remove an item!

    However, doing it in reverse order, it's not looking toward the count (minus one), it's looking toward zero which doesn't change. That's why it'll work.

    For what it's worth. :)


    Please call me Frank :)

    • Proposed as answer by IronRazerz Friday, July 19, 2013 5:47 PM
    • Marked as answer by Youen Zen Sunday, July 28, 2013 12:59 PM
    Friday, July 19, 2013 2:04 PM
  • Hi,

     Here is an example that is pretty much what Frank is telling you to do. Loop threw the items backwards and check to see if the item contains the word (Cheese). If it does then remove it.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
                If ListBox1.Items(i).ToString.Contains("Cheese") Then ListBox1.Items.RemoveAt(i)
            Next
        End Sub
    

    I am not sure if your spelling of (cheese) and (Cheeze) is a type-o or if you want to remove the items that contain ether one of them. If so then you can check to see if ether word is found and remove the item if ether one is found like this

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
                If ListBox1.Items(i).ToString.Contains("Cheese") Or ListBox1.Items(i).ToString.Contains("Cheeze") Then ListBox1.Items.RemoveAt(i)
            Next
        End Sub
    


    • Proposed as answer by Frank L. Smith Friday, July 19, 2013 5:49 PM
    • Marked as answer by Youen Zen Sunday, July 28, 2013 12:59 PM
    Friday, July 19, 2013 5:46 PM
  • Hello,

    Try the following and please note there is no effort to keep the current item selected as it may not be there after the button is pressed.

    Public Class Form1
        Private Sub Form1_Load(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles MyBase.Load
    
            ListBox1.Items.AddRange(New String() {"Cheeze Product A", "Product B Cheese", "Product Cheeze C", "Ice Cream A", "Ice Cream B"})
        End Sub
        Private Sub Button1_Click(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim items = (From T In ListBox1.Items.Cast(Of String)() Where Not T.Contains("Cheeze")).ToArray
    
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(items)
        End Sub
    End Class

    The first example will of course miss Cheese so we would need to add a second condition to the where as shown below to hit these items

    Public Class Form1
        Private Sub Form1_Load(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles MyBase.Load
    
            ListBox1.Items.AddRange(New String() {"Cheeze Product A", "Product B Cheese", "Product Cheeze C", "Ice Cream A", "Ice Cream B"})
        End Sub
        Private Sub Button1_Click(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim items = (From T In ListBox1.Items.Cast(Of String)() Where (Not T.Contains("Cheeze")) AndAlso (Not T.Contains("Cheese"))).ToArray
    
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(items)
        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.


    • Edited by KareninstructorMVP Friday, July 19, 2013 10:57 AM .................
    • Proposed as answer by IronRazerz Friday, July 19, 2013 5:47 PM
    • Marked as answer by Youen Zen Sunday, July 28, 2013 12:59 PM
    Friday, July 19, 2013 10:55 AM

All replies

  • Hello,

    Try the following and please note there is no effort to keep the current item selected as it may not be there after the button is pressed.

    Public Class Form1
        Private Sub Form1_Load(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles MyBase.Load
    
            ListBox1.Items.AddRange(New String() {"Cheeze Product A", "Product B Cheese", "Product Cheeze C", "Ice Cream A", "Ice Cream B"})
        End Sub
        Private Sub Button1_Click(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim items = (From T In ListBox1.Items.Cast(Of String)() Where Not T.Contains("Cheeze")).ToArray
    
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(items)
        End Sub
    End Class

    The first example will of course miss Cheese so we would need to add a second condition to the where as shown below to hit these items

    Public Class Form1
        Private Sub Form1_Load(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles MyBase.Load
    
            ListBox1.Items.AddRange(New String() {"Cheeze Product A", "Product B Cheese", "Product Cheeze C", "Ice Cream A", "Ice Cream B"})
        End Sub
        Private Sub Button1_Click(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim items = (From T In ListBox1.Items.Cast(Of String)() Where (Not T.Contains("Cheeze")) AndAlso (Not T.Contains("Cheese"))).ToArray
    
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(items)
        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.


    • Edited by KareninstructorMVP Friday, July 19, 2013 10:57 AM .................
    • Proposed as answer by IronRazerz Friday, July 19, 2013 5:47 PM
    • Marked as answer by Youen Zen Sunday, July 28, 2013 12:59 PM
    Friday, July 19, 2013 10:55 AM
  • Sid,

    If you want to do it through iteration, then do it in reverse order:

    For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

    Then in the loop, test to see if ListBox1.Items(i).Contains("term to search for") and if so, use .RemoveAt(i).

    The reason why you can't do it in forward order is that you're changing the count. It normally would move toward that count (minus one), but you just changed that count when you remove an item!

    However, doing it in reverse order, it's not looking toward the count (minus one), it's looking toward zero which doesn't change. That's why it'll work.

    For what it's worth. :)


    Please call me Frank :)

    • Proposed as answer by IronRazerz Friday, July 19, 2013 5:47 PM
    • Marked as answer by Youen Zen Sunday, July 28, 2013 12:59 PM
    Friday, July 19, 2013 2:04 PM
  • Hi,

     Here is an example that is pretty much what Frank is telling you to do. Loop threw the items backwards and check to see if the item contains the word (Cheese). If it does then remove it.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
                If ListBox1.Items(i).ToString.Contains("Cheese") Then ListBox1.Items.RemoveAt(i)
            Next
        End Sub
    

    I am not sure if your spelling of (cheese) and (Cheeze) is a type-o or if you want to remove the items that contain ether one of them. If so then you can check to see if ether word is found and remove the item if ether one is found like this

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
                If ListBox1.Items(i).ToString.Contains("Cheese") Or ListBox1.Items(i).ToString.Contains("Cheeze") Then ListBox1.Items.RemoveAt(i)
            Next
        End Sub
    


    • Proposed as answer by Frank L. Smith Friday, July 19, 2013 5:49 PM
    • Marked as answer by Youen Zen Sunday, July 28, 2013 12:59 PM
    Friday, July 19, 2013 5:46 PM