Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Using a search engine on the internet is second nature to most consumers. Over the last decade, search has moved into mainstream applications too. There is a feature in many search engines called Incremental Search that is very popular with consumers. I'll show you two examples of search. I will switch to my browser and let's talk about Google Search. Google has an incremental search. When I type in their search box, two things happen. On the bottom-half of the screen, Google is loading in suggested links, and in the text box itself is suggesting the next word.
I can now press Tab to auto-complete the word. This feature goes by many different names in .NET; Autocomplete, type-ahead or Incremental Search. Visual Studio has AutoComplete too. I will switch to my Visual Studio. I'm in a project called UsingAutoComplete and I am on a code page. As I type words here, Microsoft is loading a list control full of content, and as I start typing more letters, it filters it down to one word. Now, I can press the Tab key again to auto-complete.
I'll show you a few of the auto- completion features in Silverlight. I will start by looking at this ListControlsPage. This contains two list controls; the ListBox, and the ComboBox. These controls, by default, support Incremental Search. I'll add some data to both these controls. I will show you the code for that by pressing F7. Down here on line 20, I am declaring a variable of type list of string, and I am loading it with the days of the week. Then, I take that list and I assign it to the ItemsSource of the two controls; the comboBox and the listBox.
Without writing any additional code, my list controls are search-aware. I will press F5 to run the application. I will click on this List Controls button. Then, I'll select an item in my list box. Now, I'm going to type some letters. I will type the letter T, and the letter M, and the letter F, and you can see it's finding those days. Now, our only search is for the beginning of the word. That's one of the limitations of the search mechanism. It's also case-insensitive and you cannot customize the search strategy. It always uses the 'starts with' pattern, and you cannot search through more complex scenarios like searching for a product name and product ID on the same business type.
If you want more search features, use the AutoCompleteBox control. I will return back to Visual Studio, and open this AutoCompletePage. My user interface consists of three AutoComplete boxes. Each of these controls has a custom list of words to use for auto-completion. You add these words to the control in your code behind. I will show you that code by pressing F7. My first AutoComplete box is using the same list of day names that I showed you in the previous example. The code is nearly identical.
When I return back to my XAML page, I'll show you the FilterMode setting. This first AutoCompleteBox is using a FilterMode of StartsWith. What that means is as I type text in the control, it uses the look-up words I just showed you, the day names of the week, and sees if the letter I'm typing matches the starting letters of any of the words in those lists. I will run the application to show you. Click here on the AutoCompleteBox button, and go to this first text control, and type-in F for Friday, T for Tuesday, and so on.
The auto-completion portion happens when I press the Tab key. I will press Tab, and you will notice that it will finish my completion and move to the next control. My second AutoCompleteBox is using a slightly different FilterMode. It's using the Contains mode. What this does is search the inner content of the word list. The word list is coming from the month names, and I thought I would show you a technique for getting the month names from the .NET Framework. I will press F7 to switch to my code behind, and here, I am creating a variable that's going to hold a list of strings.
And then, I am writing a for each loop here, from 0 to 11, and I am calling the CultureInfo class method called DateTimeFormat.MonthNames. Then, I'm asking the CultureInfo object for the MonthName through this MonthName property. Once I've populated this list, I can then set that as an ItemSource. Press F5 to run the application, and I'll start typing in Month Names like December. Notice that I can type in partial names, I can type in em and it finds all the months that have those letters in the center of the word.
I am going to return back to my code. I'm going to comment out this line, and uncomment this line, and this line here. I want to talk about culture again. Silverlight supports all the cultures available in the .NET Framework. The example I showed used the culture information from my computer. If you want to switch to a different culture, use the appropriate culture code. So what I'm doing here is I am instantiating a new CultureInfo object, and I am using the culture code for Vietnamese and then I am using that to load the month names, and then run this application again.
I will type the letter A. You see that the Month Names are returning the culture I specified. For my last demo, I will look at a custom search algorithm. I'm going to use my own class that I created called NumberName. It contains a string called EnglishName, and NumberValue. It also has a method down here called ToString which returns the EnglishName. I then populate this class up here, and load it into a list of NumberNames. So you can see I have the number 1, the word number one, and the numeric value for that.
What I want to do is search on the word or search with the number value. In my XAML, I go to my AutoCompleteBox and I set the FilterMode to Custom. Then, I go to my code behind page, and I set up my custom filter here. I say this number has an ItemFilter. Let's sort in this function called GetWordsByNumber, and this is a predicate function which means it has to return a Boolean value. So this takes the search string and the currentNumberName and the code in here attempts to parse the number and see if I typed a numeric value 2, if that matches the numeric value here.
Press F5 to run the application. Come down here and type-in one, I got a match, tw, I got a match. Now, I'm going to type the numeric value of 2 and I get a match. Now, I will use the Down arrow and then the Tab key. These examples should give you some ideas of ways to search through your own application data.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101672 Viewers
61 Video lessons · 88422 Viewers
71 Video lessons · 72266 Viewers
56 Video lessons · 103990 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.