tags:

views:

222

answers:

2

Hi,

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
A: 

Create a List<string> with all your items in it and search that list, then only add the results of the search to your listbox.

Off course you'd have all items in your listbox to start with, but then also in the List. Then clear your listbox and search the list for the item entered in the textbox. now you can add the search results to your listbox.

Tony
any sample please?
JPro
But the thing is I am reading approximately 3-7 mb file and creating a list with all these items?.... does it hang my application?
JPro
I have included my entire code
JPro
Weren't you already loading that into the Listbox?
Martinho Fernandes
I am loading the file through dragdrop into listbox
JPro
Tony
@Tony: but I open the file eveytime the user searches, it is going to delay my application response. right? he has to stare at the application not knowing when whould it finish
JPro
Tony
+1  A: 

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).

Dan Tao
Thanks @dan, this is what I had in mind, although my plan was a bit longer, since I was going to remove all items and search a list with the items in it and add any matches. So your way of doing is even better! :)
Tony
Thank you for the sample.
JPro