filter items in textfile?

Pissedelf

New member
Joined
Jun 27, 2013
Messages
2
Programming Experience
Beginner
Hi all, I am working on a program and am running into some difficulty. I'm hoping someone with more expertise may be able to assist.

I am importing a textfile into my code as an array. The textfile contains a large list of objects. This array is populating a list box within the form.

I also have a text box that I should be able to enter a letter into. When I enter a letter, say P, and then press a button, I want every item within the textfile that starts with the letter "P" to populate the list box.

*this is where things are not working*

I've tried to explain things as best I could, but if you need better clarification let me know. Any help would be greatly appreciated.
 
Load the file contents into a String array by calling File.ReadAllLines. Each time you want to filter, you can start with an empty List(Of String) and loop through the array, adding each element that matches to the list. To find a match, call String.StartsWith to check whether the element starts with the text in the TextBox. You should probably use the overload of StartsWith that lets you specify a case-insensitive comparison.
 
Using Linq:

MyListBox.Datasource = (From r In ResultsList
                        Where If(MyTextBox.Text.IsNullOrWhiteSpace, True, r.StartsWith(MyTextBox.Text, StringComparison.CurrentCultureIgnoreCase))
                        Select r).ToList


You could also change the StartsWith method with Contains, if you want to search string contents, not just the beginning.
 
Last edited:
Using Linq:

MyListBox.Datasource = (From r In ResultsList
                        Where If(MyTextBox.Text.IsNullOrWhiteSpace, True, r.StartsWith(MyTextBox.Text, StringComparison.CurrentCultureIgnoreCase))
                        Select r).ToList


You could also change the StartsWith method with Contains, if you want to search string contents, not just the beginning.

Not that there's anything wrong with your code but, for someone posting their first question who doesn't know how to do this, using LINQ might be a bit advanced if they don't actually know the "standard" code that the LINQ is replacing. I agree that LINQ is the best way to write clean code to do this sort of thing but you should really only use LINQ if you already know how to do something without it. Otherwise you're neglecting the fundamentals.
 
Thank you Jmcilhinney and Herman for the quick responses

Herman, your code was right on the money! After wrestling with it for a few minutes, I finally got everything running great.

The assistance was very much appreciated
 
The only thing I would do to make it more readable is use a Lambda
MyListBox.DataSource = ResultsList.Where(Function(r)r.StartsWith(MyTextBox.Text)).Select(Function(r)r).ToList 


Jmcilhinney, LINQ is the right way to do it, even though it is easier to understand with loops etc it is bad practice. As the people asking basic questions may be new, I think it is right to show them the correct way, even if it involves a learning curve.
 
Last edited:
Back
Top