Solved

C# Ignore casing on Listbox Search

Posted on 2011-03-19
6
788 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now