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

Search items from text file

scriptick
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
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
Oct 24 '13 #1
Share this Question
Share on Google+
6 Replies


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

-Frinny
Oct 24 '13 #2

scriptick
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" );
  4.  
  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

100+
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

scriptick
P: 15
@vijay6
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

100+
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

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

[C#]
Expand|Select|Wrap|Line Numbers
  1. public List<string> AllWordOptions {
  2.     get {
  3.  
  4.         string pathToFile = "options.txt";
  5.         List<string> lines = null;
  6.  
  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.         }
  16.  
  17.         return lines;
  18.     }
  19. }
[VB.NET]
Expand|Select|Wrap|Line Numbers
  1. Public ReadOnly Property AllWordOptions As List(Of String)
  2.   Get
  3.  
  4.     Dim pathToFile As String = "options.txt"
  5.     Dim lines As List(Of String) = Nothing
  6.  
  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
  16.  
  17.       Return lines 
  18.  
  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.



-Frinny
Nov 4 '13 #7

Post your reply

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