-1

/*I am reading many files and getting data through File.ReadAllLines. Now I want to search in these files for a specific string written in a textbox. Whenever I put some text in the textbox it must return lines of text containing that word. I am coding in textchanged property but it is not successful as it gives me a result even when I press backspace or add any other word. */

I have successfully made it to work. I was clearing the listbox every time it runs else statement. Now I just want you people to tel me what should I do to make it work fast. 
Fun Haha
  • 1
  • 1
  • So what do you expect to happen when the user clears the textbox? – Sten Petrov Apr 02 '13 at 19:39
  • If you don't want certain characters/key strokes to trigger the change event you will have to explicitly tell it so. At the moment your code is not executing only when the textbox is empty – Steven Marciano Apr 02 '13 at 19:39
  • First of all TextChanged is not a property. It is one of the events of TextBox. When anything changes on your TextBox it fires TextChanged event. When would you like to get the result? – Dilshod Apr 02 '13 at 19:40
  • 1
    Also `lines[i].ToString()....` is poorly written. `lines` is already an array of strings, `ToLower()` creates a temp copy for each line(slow, memory intense). Rewrite as: `lines[i].IndexOf(textbox1.Text, StringComparison.InvariantCultureIgnoreCase)>=0` – Sten Petrov Apr 02 '13 at 19:44
  • @StenPetrov Thanks for providing fast solution. It helped. Can you mention anything else which may be slowing it down because I got to search from the lot of files and they contain a lot of lines. – Fun Haha Apr 03 '13 at 06:40

1 Answers1

0

if you don't want to get the result right away when you type something in Textbox then put Button on your form and try this code:

string[] lines=File.ReadAllLines(path);
var result = lines.Where(l => l.Contains("text")).ToList();

I hope this helps

Dilshod
  • 3,189
  • 3
  • 36
  • 67