By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,054 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,054 IT Pros & Developers. It's quick & easy.

Search items from text file

P: 15
Suppose I have a textfile on C://file.txt
On that text file contains -
Expand|Select|Wrap|Line Numbers
  1. Buoyes 
  2. bytes
  3. Bubble
  4. Coogle
  5. Code
  6. Cripsy
  7. Cyclone
  8. Crime
  9. Criminal
  10. Dog
  11. Din
  12. Done
  13. Doing
  14. Duty
  15. Desk
  16. Eagle
  17. Ear
  18. Eat
  19. .......
  20. etc
Now my question is -
I have a csharp winform application
I wanna put a code on button1-
If I write in textbox1= E
On listbox show all Word started with E from
If I type "Do"
then show
How to do this?
Thanks in advanced
Oct 24 '13 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 5K+
P: 9,731
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:
Expand|Select|Wrap|Line Numbers
  1. from option in loadedStrings
  2. where option.Contains(Textbox1.Text)
  3. select option
Bind your ListBox to that sub list of strings.

Oct 24 '13 #2

P: 15
can you pls check this code?
I found this but not working....

Expand|Select|Wrap|Line Numbers
  1.  protected void Button_Click( object sender, EventArgs e)
  2. {
  3.   string fileLoc = Server.MapPath( "You Location" );
  5.   if (File.Exists(fileLoc))
  6.   {
  7.     String line;
  8.     using (TextReader tr = new StreamReader(fileLoc))
  9.     {
  10.       line = tr.ReadToEnd();
  11.     }
  12.     string[] read=line.StartsWith(textboxName.text);
  13.     for( int i=0;i<read.length;i++)
  14.     {
  15.       //your logic;
  16.     }
  17.   }
  18. }
Oct 26 '13 #3

P: 158
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.
Oct 28 '13 #4

P: 15
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?
Nov 1 '13 #5

P: 158
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 (:
Nov 3 '13 #6

Expert Mod 5K+
P: 9,731
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:

Expand|Select|Wrap|Line Numbers
  1. public List<string> AllWordOptions {
  2.     get {
  4.         string pathToFile = "options.txt";
  5.         List<string> lines = null;
  7.         try {
  8.             using (System.IO.StreamReader sr = new System.IO.StreamReader(pathToFile)) {
  9.                 string line = null;
  10.                 line = sr.ReadToEnd();
  11.                 lines = new List<string>(from str in line.Split(System.Environment.NewLine) select str);
  12.             }
  13.         } catch (Exception e) {
  14.             //The file could not be read
  15.         }
  17.         return lines;
  18.     }
  19. }
Expand|Select|Wrap|Line Numbers
  1. Public ReadOnly Property AllWordOptions As List(Of String)
  2.   Get
  4.     Dim pathToFile As String = "options.txt"
  5.     Dim lines As List(Of String) = Nothing
  7.     Try
  8.       Using sr As New IO.StreamReader(pathToFile)
  9.           Dim line As String
  10.           line = sr.ReadToEnd()
  11.           lines = New List(Of String)(From str In line.Split(System.Environment.NewLine) Select str)
  12.         End Using
  13.       Catch e As Exception
  14.        'The file could not be read
  15.       End Try
  17.       Return lines 
  19.   End Get
  20. 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.

Nov 4 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.