0

I have a textbox that filters listbox results, and if I type it all caps it won't return anything, but all lower will. I want to see if I missed something. Here is what I am using.

Thanks

private void Filter(object sender, FilterEventArgs e)
{

    var src = e.Item as Users;
    if (src == null)
        e.Accepted = false;
    //else if (src.Name!= null && !src.Name.Contains(Search))
    else if (src.Name!= null && !src.Name.ToLower().Contains(Search))
        e.Accepted = false;
}
1
  • The Property the Textbox is binding to Commented Apr 9, 2013 at 21:32

2 Answers 2

2

Internally string.Contains is implemented using IndexOf and an OrdinalCompare enum.
So your code could be changed to use a simple IndexOf without incurring the penalty required by the ToLower method that return a new copy of the original string.

if (src == null || string.IsNullOrEmpty(src.Name))
    e.Accepted = false;
else if (src.Name.IndexOf(Search, StringComparison.CurrentCultureIgnoreCase) < 0)
    e.Accepted = false;

Strings are immutable, meaning that once created you can't change a string.
A method like ToLower actually creates a new string object to hold the new sequence of characters of the lower case kind, and that new object is returned.

Sign up to request clarification or add additional context in comments.

Comments

2

Change your Search to lower case also

        else if (src.Name!= null && !src.Name.ToLower().Contains(Search.ToLower()))
            e.Accepted = false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.