Solved

C# Ignore casing on Listbox Search

Posted on 2011-03-19
6
785 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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