Search items from text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scriptick
    New Member
    • Apr 2013
    • 15

    Search items from text file

    Suppose I have a textfile on C://file.txt
    On that text file contains -
    Code:
    Buoyes 
    bytes
    Bubble
    Coogle
    Code
    Cripsy
    Cyclone
    Crime
    Criminal
    Dog
    Din
    Done
    Doing
    Duty
    Desk
    Eagle
    Ear
    Eat
    .......
    etc
    Now my question is -
    I have a csharp winform application
    Button1
    Textbox1
    Listbox1
    I wanna put a code on button1-
    If I write in textbox1= E
    On listbox show all Word started with E from
    file.txt
    If I type "Do"
    then show
    Done
    Doing
    Dog
    How to do this?
    Thanks in advanced
    Last edited by Frinavale; Oct 24 '13, 05:53 PM.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Load the text file into a List of String.

    Then implement a method that handles the key up event for the TextBox.

    In that method you can use LINQ to retrieve a sub list of strings that match what the text in the TextBox contains.

    For example:
    Code:
    from option in loadedStrings
    where option.Contains(Textbox1.Text)
    select option
    Bind your ListBox to that sub list of strings.

    -Frinny

    Comment

    • scriptick
      New Member
      • Apr 2013
      • 15

      #3
      can you pls check this code?
      I found this but not working....

      Code:
       protected void Button_Click( object sender, EventArgs e)
      {
        string fileLoc = Server.MapPath( "You Location" );
      
        if (File.Exists(fileLoc))
        {
          String line;
          using (TextReader tr = new StreamReader(fileLoc))
          {
            line = tr.ReadToEnd();
          }
          string[] read=line.StartsWith(textboxName.text);
          for( int i=0;i<read.length;i++)
          {
            //your logic;
          }
        }
      }
      Last edited by Frinavale; Nov 4 '13, 06:02 PM. Reason: Please use [CODE] and [/CODE] tags when posting code. This is your final warning.

      Comment

      • vijay6
        New Member
        • Mar 2010
        • 158

        #4
        Hey scriptick, you are doing it wrong. 1) You should read your text file line by line and store it in an array. 2) Or in your code after reading the file once, split the string(line) using 'Carriage Return' and store it in an array. Then use LINQ (which Frinavale mentions) to extract the words which start with your input character from that array.

        NOTE: You haven't closed your StreamReader in your code.
        Last edited by Frinavale; Oct 28 '13, 09:12 PM.

        Comment

        • scriptick
          New Member
          • Apr 2013
          • 15

          #5
          Originally posted by vijay6
          Hey scriptick, you are doing it wrong. 1) You should read your text file line by line and store it in an array. 2) Or in your code after reading the file once, split the string(line) using 'Carriage Return' and store it in an array. Then use LINQ (which Frinavale mentions) to extract the words which start with your input character from that array.

          NOTE: You haven't closed your StreamReader in your code.
          Hey vijay bro how are you? could you remember me? its pavel. However I don't understand what you say. could you pls tell me in more details?

          Comment

          • vijay6
            New Member
            • Mar 2010
            • 158

            #6
            Hey scriptick, in your code after line number 12, line(string) contains the entire text of the text file. And in line number 16 you are checking whether the line(string) is started with the inputted text or not. You asked something and you done something else.

            By the way thanks for asking. 'm good, hope you too good (:

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Check out this article on How to read text from a file.

              This is how you would read the text in the file you posted and store the words into a list of strings:

              [C#]
              Code:
              public List<string> AllWordOptions {
              	get {
              
              		string pathToFile = "options.txt";
              		List<string> lines = null;
              
              		try {
              			using (System.IO.StreamReader sr = new System.IO.StreamReader(pathToFile)) {
              				string line = null;
              				line = sr.ReadToEnd();
              				lines = new List<string>(from str in line.Split(System.Environment.NewLine) select str);
              			}
              		} catch (Exception e) {
              			//The file could not be read
              		}
              
              		return lines;
              	}
              }
              [VB.NET]
              Code:
              Public ReadOnly Property AllWordOptions As List(Of String)
                Get
                
                  Dim pathToFile As String = "options.txt"
                  Dim lines As List(Of String) = Nothing
              
                  Try
                    Using sr As New IO.StreamReader(pathToFile)
                        Dim line As String
                        line = sr.ReadToEnd()
                        lines = New List(Of String)(From str In line.Split(System.Environment.NewLine) Select str)
                      End Using
                    Catch e As Exception
                     'The file could not be read
                    End Try
              
                    Return lines 
              
                End Get
              End Property
              The list of strings should be a Private member of the page. Really they should be a static member that is only loaded once for the application but for now you could just set it as a private member of the page.

              Once you have the list populated, you can then handle the text changed event for your text box and use LINQ to return any matching results.



              -Frinny
              Last edited by Frinavale; Nov 4 '13, 06:12 PM.

              Comment

              Working...