.net – clear listbox items except for searched items vb.net

netvb.net

I am using the below code to find all the items in a listbox using vb.net 2005. But how can remove the non searched items from the listbox after searching?

EDIT : I included the entire code

 Imports System.IO

Public Class Form1
    Public Sub New()

         InitializeComponent()
        ListBox1.SelectionMode = SelectionMode.MultiExtended


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim reader As StreamReader = New StreamReader("input.txt")
        Try
            Me.ListBox1.Items.Clear()
            Do
                Me.ListBox1.Items.Add(reader.ReadLine)
            Loop Until reader.Peek = -1

        Catch
            Me.ListBox1.Items.Add("File is empty")

        Finally
            reader.Close()
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.BeginUpdate()
        ListBox1.SelectedIndices.Clear()
        If TextBox1.Text.Length > 0 Then
            For index As Integer = 0 To ListBox1.Items.Count - 1
                Dim item As String = ListBox1.Items(index).ToString()

                If item.IndexOf(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) >= 0 Then

                    ListBox1.SelectedIndices.Add(index)
                End If
            Next
        End If
        ListBox1.EndUpdate()
    End Sub

    Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As  _
 System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

    Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim MyFiles() As String

            Me.ListBox1.Items.Clear()

            MyFiles = e.Data.GetData(DataFormats.FileDrop)
            Using sr As StreamReader = New StreamReader(MyFiles(0))
                Dim line As String

                Do
                    line = sr.ReadLine()

                    ListBox1.Items.Add(line)
                Loop Until line Is Nothing

            End Using

        End If
    End Sub
End Class

Best Answer

It sounds to me like you want something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ListBox1.BeginUpdate()

    Try
        ' keep track of the "non-searched items" '
        Dim indicesToRemove As New List(Of Integer)

        ListBox1.SelectedIndices.Clear()
        If TextBox1.Text.Length > 0 Then
            For index As Integer = 0 To ListBox1.Items.Count - 1
                Dim item As String = ListBox1.Items(index).ToString()

                If item.IndexOf(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) >= 0 Then
                    ListBox1.SelectedIndices.Add(index)
                Else
                    ' this item was not searched for; we will remove it '
                    indicesToRemove.Add(index)
                End If
            Next

            ' go backwards to avoid problems with indices being shifted '
            For i As Integer = indicesToRemove.Count - 1 To 0 Step -1
                Dim indexToRemove As Integer = indicesToRemove(i)
                ListBox1.Items.RemoveAt(indexToRemove)
            Next
        End If
    Finally
        ListBox1.EndUpdate()
    End Try
End Sub

Notice I also put the call to ListBox1.EndUpdate() in a Finally block. In my experience this is a good practice, as it ensures your control will return to a normal state even in the event of an exception (which you can still handle however you like).

Related Topic