Solved

C# Ignore casing on Listbox Search

Posted on 2011-03-19
6
802 Views
Last Modified: 2012-05-11
Howdy all,

I have two questions/problems:-

I have  listbox populated with peoples Names, I then have a textbox for performing an incremental search of those names.As the user types it filters out irrelevant data. This works fine.

However it doesn't currently ignore casing, so a person named 'John' in the list would not show up if I typed 'john' into Search. I basically want my search function to ignore the casing.

After some research there seems several ways to impliment this, either convert all strings to same case or use StringComparison, neither of which ive acturally sucessefully made work.

      private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            
            string searchTerm = tbSearch.Text;
            List<string>searchList = new List<String>();
            searchList.Clear();
            foreach (ContactDetails c in contactlist)
        {
                   if (!string.IsNullOrEmpty(tbSearch.Text))
                    {
                       if (c.Name.Contains(searchTerm))
                        {
                            searchList.Add(c.Name );
                           
                        }
                    }
                }

            
      if (searchList.Count > 0)
            {
                listNames.Visibility = Visibility.Hidden;
                listBox1.Visibility = Visibility.Visible;
                listBox1.ItemsSource = searchList;
            }

            else if (tbSearch.Text.Equals(""))
            {
                listNames.Visibility = Visibility.Visible;
                listBox1.Visibility = Visibility.Hidden;
            }
            else
            {
               
               listBox1.ItemsSource = autoList;
               listBox1.Visibility = Visibility.Visible;
          
            }      
        }

Open in new window


Issue 2:-  The eagle eyed amoungst you will see that I have:-

  listNames.Visibility = Visibility.Visible;
                listBox1.Visibility = Visibility.Hidden;

Open in new window


This is because the orginal list (listNames) has an itemSource called 'contactlist'.
Now in order for the searchList to populate 'listNames', at somepoint I need to clear the 'contactlist' from listNames itemSource....which again I couldn't figure out.

So instead I just copied  listNames listbox, which became 'listBox1' and assigned its itemSource to autoList and adjusted the visibility for each listbox in each situation the textsearch provided

My question is, wether this is a genius idea or more like a complete botch....and if its a complete botch and would, with lots contacts in the list effect performance. how can i streamline it and do the same thing programatically.

Many Thanks

John
0
Comment
Question by:Bronco78th
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:Amick
ID: 35171662
Ordinarily you would normalize the case for the search key and the searched text in order for the search to appear case insensitive.
0
 
LVL 12

Expert Comment

by:Amick
ID: 35171672
You may also want to consider using System.String.Compare as described here:

http://en.csharp-online.net/CSharp_FAQ:_How_perform_a_case_insensitive_string_comparison
0
 

Author Comment

by:Bronco78th
ID: 35172714
So assuming I have

CultureInfo.CurrentCulture.CompareInfo.Compare
                               (tbSearch.Text, c.Name,
                               CompareOptions.IgnoreCase);

what which point should I put this in the above code, because it still aint working and I havent got any errors.

Many Thanks,

John
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 12

Accepted Solution

by:
Amick earned 125 total points
ID: 35175516
This code in comment 35172714 is doing a straight compare rather than a contains as in line 11 of your example.  You may want to trim the whitespace from both edges using the .Trim() method before comparing them if the nature of your data is such that whitespace is what is causing the problem.

Also consider just using a .ToLower() or .ToUpper() on both of the components of the search and continuing to use contains.
0
 

Author Comment

by:Bronco78th
ID: 35176028
Cheers, Amick

One final point, sorry I am a nooby, but more of a grneral question is it possible to use the LowerTo and trim functions in one go? as in something like:-

 string lower = searchTerm.ToLower() + searchTerm.Trim() ;
                string lowerName = c.Name.ToLower() + c.Name.Trim();

Open in new window


Or do i acturally need to create several strings like :-

string lower = searchTerm.ToLower();
                string lowerName = c.Name.ToLower();

                string trimLower = lower.Trim();
                string trimName = lowerName.Trim();

Open in new window


Anyway got it going, answer that alst bit and you get the points :)

Many Thanks

John




0
 
LVL 12

Expert Comment

by:Amick
ID: 35178295
I believe you can do:

string lower = searchTerm.ToLower().Trim();
string lowerName = c.Name.ToLower().Trim();

Open in new window

Use lower and lowerName as your comparison strings.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question