Results 1 to 13 of 13

Thread: [2005] Read text file into a listbox. (Please go to other topic)

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    [2005] Read text file into a listbox. (Please go to other topic)

    Please go to my other topic here.
    Last edited by Deadly Asphyxiation; May 20th, 2008 at 08:59 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Read text file into a listbox.

    First of all, you're comparing a line of text from the file with the LENGTH of the text entered by the user. What do you think Len does?
    Code:
                letter = Len(txt_search.Text)
                If sr.ReadLine = letter Then
    You should be comparing the line to the Text of the TextBox, not the length of the Text in the TextBox.

    Furthermore, if that comparison ever did hold true you would not then add that line to the ListBox but rather you go and read the next line and add that. You should be reading a line and assigning the result to a variable. You can then compare that variable to the contents of the TextBox and, if the match, add that variable's value to the ListBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: [2005] Read text file into a listbox.

    Okay, I've changed a bit of this, here's my new code:

    Code:
        Private Sub txt_search_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_search.TextChanged
            lbl_Welcome.Visible = False
            lbl_WelcomeMessage.Visible = False
            btn_Add2.Visible = False
            lst_results.Visible = True
    
            Do
                While Not sr.EndOfStream
                    tempresult = sr.ReadLine
                    If tempresult = txt_search.Text Then
                        lst_results.Items.Add(tempresult)
                    End If
                End While
            Loop
        End Sub
    But now my program will run until I enter a character into the textbox search. Then it freezes ...

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: [2005] Read text file into a listbox.

    I think it would probably be easier if I should the text file.

    Code:
    404 error
    An error message received when you try to access a web page that either doesn't exist or is 
    
    unavailable at the address you gave.
    80211a, 80211b, 80211g, 80211i, 80211n
    Different standards for operating wireless networks (Wi-Fi). Loosely the higher the letter, 
    
    the faster the speed. So far they are mostly incompatible with each other. 
    80286, 80386, 80486 (or 286, 386, 486)
    Three generations of PC processor, now pretty much obsolete, the ancestors of Intel's 
    
    Pentium. The 286 was the earliest processor able to run (just about) a version of Windows, 
    
    although the 386 was really the minimum to run it properly. The 486 was the earliest 
    
    processor able to run Windows 95 - just about. Machines of this vintage cannot run modern 
    
    versions of Windows at all, but many are still in use as basic word-processors or where a 
    
    dedicated machine is required which doesn't need to be powerful.
    Basically, where you can see 404, and ActiveX I want to search for those strings. Not the description, the name. I want to use real time searching, so if there are two words, Antelope and Arrows, you type in an 'A' both will show, if you type an 'r' it will only show arrows, as antelope starts 'An'. Can anyone help me here? I'm desperate.

    Many many thanks in advance.

    The part I am most stuck on is here:

    Code:
    Do While Not sr.EndOfStream
                tempresult = sr.Peek
                If tempresult = txt_search.Text Then
                    lst_results.Items.Add(sr.ReadLine)
                End If
            Loop
    If I put that into pseudo code:
    Code:
    While the reading is not at the end of the text file
    tempresult = Next character in file
    if temresult = txt_search.text Then
    Add the line 'sr' is reading to the listbox.
    Last edited by Deadly Asphyxiation; May 15th, 2008 at 10:01 AM.

  5. #5
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] Read text file into a listbox.

    It sounds like you are looking for some sort of auto complete TextBox property. No need to write it from scratch. See here: http://msdn.microsoft.com/en-us/libr...de(VS.80).aspx

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: [2005] Read text file into a listbox.

    No that only works for a textbox, I want a real-time search of MY OWN TEXT FILE. I'm not sure which bit I'm doing wrong though ...

  7. #7
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] Read text file into a listbox.

    Your user isn't typing the search word in a TextBox? I guess I'm a bit confused as to what you are doing. At any rate, you could use the contents of your text file to fill the AutoCompleteSource of a TextBox:
    vb.net Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.  
    6.         Dim sr As New StreamReader("C:\myFile.txt")
    7.  
    8.         Dim s As String = sr.ReadToEnd()
    9.         Dim words() As String = s.Split(" "c)
    10.  
    11.         Dim col As New AutoCompleteStringCollection()
    12.         col.AddRange(words)
    13.  
    14.         Me.TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
    15.         Me.TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
    16.         Me.TextBox1.AutoCompleteCustomSource = col
    17.  
    18.     End Sub
    19. End Class
    It's only a start, but now every word from your text file is in the AutoCompleteSource of your TextBox.

    Good luck on your project.

  8. #8
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: [2005] Read text file into a listbox.

    If I'm not mistaken, the reason it freezes is because you've entered into an infinite loop. Make sure to include code that at some point will advance and eventually exit a loop.
    Code:
     
            Do
                While Not sr.EndOfStream
                    tempresult = sr.ReadLine
                    If tempresult = txt_search.Text Then
                        lst_results.Items.Add(tempresult)
                    End If
                End While
            Loop
    Basically, you have a loop inside of a loop, and the Outer loop has no way of exiting.
    The Do Loop is causing the freezing problem.

    If you want to keep the Do Loop, possibly do something like
    Code:
     
            Do
                    tempresult = sr.ReadLine
                    If tempresult = txt_search.Text Then
                        lst_results.Items.Add(tempresult)
                    End If
            Loop While Not sr.EndOfStream
    Last edited by Vane; May 16th, 2008 at 10:02 AM.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: [2005] Read text file into a listbox.

    Quote Originally Posted by nmadd
    Your user isn't typing the search word in a TextBox? I guess I'm a bit confused as to what you are doing. At any rate, you could use the contents of your text file to fill the AutoCompleteSource of a TextBox:
    vb.net Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.  
    6.         Dim sr As New StreamReader("C:\myFile.txt")
    7.  
    8.         Dim s As String = sr.ReadToEnd()
    9.         Dim words() As String = s.Split(" "c)
    10.  
    11.         Dim col As New AutoCompleteStringCollection()
    12.         col.AddRange(words)
    13.  
    14.         Me.TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
    15.         Me.TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
    16.         Me.TextBox1.AutoCompleteCustomSource = col
    17.  
    18.     End Sub
    19. End Class
    It's only a start, but now every word from your text file is in the AutoCompleteSource of your TextBox.

    Good luck on your project.
    That would use up a lot of memory as you are dimming a string as my whole text file. The text file is pretty big as far as a text file goes and I don't want to place it into a string. I want a real-time search of the text file, if someone presses "A" and the textbox reads: "A" then it will return ALL words with the ASCII character "A", if they then press "c" and it reads "Ac" it will only return the search results of "Ac".

    Quote Originally Posted by Vane
    If I'm not mistaken, the reason it freezes is because you've entered into an infinite loop. Make sure to include code that at some point will advance and eventually exit a loop.
    Code:
     
            Do
                While Not sr.EndOfStream
                    tempresult = sr.ReadLine
                    If tempresult = txt_search.Text Then
                        lst_results.Items.Add(tempresult)
                    End If
                End While
            Loop
    Basically, you have a loop inside of a loop, and the Outer loop has no way of exiting.
    The Do Loop is causing the freezing problem.

    If you want to keep the Do Loop, possibly do something like
    Code:
     
            Do
                    tempresult = sr.ReadLine
                    If tempresult = txt_search.Text Then
                        lst_results.Items.Add(tempresult)
                    End If
            Loop While Not sr.EndOfStream
    I understand what you mean about the continuous looping, however, I still have no results. I can't seem to get anything, I've tried a couple of other searches as well and still get nothing. Here's an update of my code now:

    Code:
        Private Sub txt_search_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_search.TextChanged
            HideWelcomeScreen()
            HideAddItems()
            lst_results.Visible = True
    
            Do
                tempresult = sr.ReadLine
                If tempresult = txt_search.Text Then
                    lst_results.Items.Add(sr.ReadLine)
                End If
            Loop While Not sr.EndOfStream
        End Sub

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: [2005] Read text file into a listbox.

    After doing a little research and finding sr.peek and sr.read I've come up with a new solution.

    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
    That code in theory should make a real-time search engine, although I have a problem now. The line:

    Code:
    Loop While Not sr.Peek = " " Or Not sr.Peek = "," And counter_search < letter
    Has an error, which reads:

    Conversion from string " " to type 'Double' is not valid.

    This error is pointing to the word: "letter"

    Can anyone help me here?

    I think I'm nearly done.
    Last edited by Deadly Asphyxiation; May 19th, 2008 at 06:23 AM.

  11. #11
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: [2005] Read text file into a listbox.

    I'm not quite sure if this would work, but you can try converting letter to an int.

    Code:
    Loop While Not sr.Peek = " " Or Not sr.Peek = "," And counter_search < Convert.ToInt16(letter)
    Looking at Peek in VB, it says it's an integer property. Which could possibly be a problem as you're comparing it to a string.

    Possible to use convert or use .ToString as well.

    Code:
    Loop While Not sr.Peek.ToString = " " Or Not sr.Peek.ToString = "," And counter_search < letter
    Code:
    Loop While Not Convert.ToString(sr.Peek) = " " Or Not Convert.ToString(sr.Peek) = "," And counter_search < letter
    Worth a try.

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    56

    Re: [2005] Read text file into a listbox.

    Thanks Vane, but that didn't work either, I tried all three pieces of code and none yielded the result. They all turned the form white, and only converting letter to an int produced an error message ... Thanks for trying anyway.

  13. #13
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: [2005] Read text file into a listbox.

    Bummer. The only thing I can really suggested then is possibly using Breakpoints and slowly working your way through the code, looking at the values each variable gets and sends.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width