Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET File I/O and Registry > Reading from a text file into a listbox


Reply
 
Thread Tools Display Modes
  #1  
Old 05-19-2008, 05:31 AM
Atamagaokashii Atamagaokashii is offline
Newcomer
 
Join Date: May 2008
Posts: 1
Default Reading from a text file into a listbox

Hello, I'm trying to read from a text file into a listbox.

I want to do this via a real-time search engine.
So when the user press "A" it will show all results with "A", however when the user adds a "c" it will only show results with "Ac". This, so far, is my code:

Code:
        HideWelcomeScreen()
        HideAddItems()
        lst_results.Visible = True
        lst_results.Items.Clear()
        counter_search = 0
        letter = Len(txt_search.Text)

        Using sr As New StreamReader("Dictionary.txt")
            For counter_search = 1 To letter
                nextletter = sr.Peek
                If Not nextletter = " " And Not nextletter = "," Then
                    tempresult = tempresult + sr.Read
                    counter_search += 1
                End If
            Next
        End Using
        sr.Close()

        Using sr As New StreamReader("Dictionary.txt")
            Do
                If tempresult = sr.ReadLine And Not txt_search.Text = "" Then
                    lst_results.Items.Add(tempresult)
                End If
            Loop While Not sr.EndOfStream
        End Using
However, the form just goes white, and nothing else happens.

Can anyone help me here?

I think I'm nearly done.
Reply With Quote
  #2  
Old 05-19-2008, 11:42 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Ultimate Contributor

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
Default

This is a synchronous process. The computer can only do one thing at a time. What happens is while you're busy looping over all of the values in the file, you're keeping the main thread busy, which means it's not able to respond to resizes, clicks, user input, etc. Your best bet for maintaining responsiveness is going to be to offload the filtering duties to a worker thread. However, you have worse problems: your filtering logic does nothing at all close to what it should do.

To filter, you will be asking the question, "Does this word start with these letters?" for each word in the list. Instead, here is what your code does:
Code:
For 1 to the number of letters in the search box
    If the next letter is not a space or comma
        add the next character to tempResult
        increment counter_search
    End If
Next
This block will leave you with the first letter letters in dictionary.txt; the string will possibly contain a line break but your file might be entirely comma-delimited. Assuming a search value of "ac", if the file starts with "apple" you're going to store "ap" in tempResult. Needless to say, when you get to the next loop, it's very unlikely that an item that matches will be found and you get nothing. This is why the form goes white; you are telling the computer the wrong thing to do.

It'd be much better to first get a list of words from the file, then go over this list of words. You'll still need some asynchronous processing to make it work without appearing to lock up. I say get the filtering working first. It'll be much easier if you define a GetWord() method that can pull the next word from the stream. It sounds fun to implement as an example, but I don't really have time today.

Nitpicker's Corner
Code:
        Using sr As New StreamReader("Dictionary.txt")
            ...
        End Using
        sr.Close()
This is wrong, and shouldn't compile unless you are making a different mistake. The Using statement closes the stream for you, and also limits the scope of the variable sr to its own block. Remove the Close call.
Reply With Quote
  #3  
Old 05-19-2008, 12:43 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Ultimate Contributor

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
Default

Actually, it's pretty fun to implement. I've attached a project that contains a user control that consists of a text box and a list box. You add items to the list box, and as the user types in the text box the list box is filtered. It's really basic and lacks a bunch of functionality, but I think it's a good place to start.

The default program creates a word list based on the files in your My Documents directory; on my machine this was about 11,000 files, so I figure it's a good test of performance. This does mean that on the first program launch, it has to create the word list which will take quite some time, so be patient. The format of the word list is CSV with no quoting, that is "," cannot be part of any values.

The way the filter box works is simple. When you add items to it, it keeps track of the full list of items in a private list. When the user types in the text box, a timer is started; if the timer elapses before the user types another character, we ask the background worker to filter the word list for us. When the list has been filtered, we first remove all items in the current list then add the new, filtered list. Since the full list is always stored privately, we can add those items back by filtering with less restrictive text.

There's still a slight delay after filtering completes, as the list box's items can only be manipulated from the UI thread. With 11,000 items, I perceived less than 2 seconds of delay. I consider this acceptable, and I figure it'd be most wise to only filter using a few hundred items anyway.

It'd be nice if this were more generic to reflect the fact that ListBox can contain any Object, but that would have complicated the example and made it take a few hours, rather than about 20 minutes. If you need more functionality, feel free to expand it.
Attached Files
File Type: zip FilteringBoxDemo.zip (18.6 KB, 12 views)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb