[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 817
  • Last Modified:

C# Ignore casing on Listbox Search

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
Bronco78th
Asked:
Bronco78th
  • 4
  • 2
1 Solution
 
AmickCommented:
Ordinarily you would normalize the case for the search key and the searched text in order for the search to appear case insensitive.
0
 
AmickCommented:
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
 
Bronco78thAuthor Commented:
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
Independent Software Vendors: 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!

 
AmickCommented:
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
 
Bronco78thAuthor Commented:
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
 
AmickCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now