?
Solved

C# Ignore casing on Listbox Search

Posted on 2011-03-19
6
Medium Priority
?
806 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
Amick earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 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