3.6. Create Text Boxes thatAuto-Complete
In many of the nooks and crannies of the Windows operating system, you'll find AutoComplete text boxes. These text boxes suggest one or more values as you type.
Note: With . NET's new auto-complete features, you can create intelligent text boxes able to suggest possible values based on recent entries or a default list.
Usually, AutoComplete values are drawn from your recent history. For example, when you type a URL into Internet Explorer's address bar, you'll see a list that includes URLs you've surfed to in the past. Now with .NET 2.0, you can harness the same AutoComplete features with your own custom lists or one of the lists maintained by the operating system.
3.6.1. How do I do that?
The TextBox and the ComboBox controls both support the AutoComplete feature in .NET 2.0. To use AutoComplete, first set the control's AutoCompleteMode property to one of the following values:
After choosing the type of AutoComplete, you need to specify what list will be used for suggestions. Do this by setting the AutoCompleteSource property to one of the following values:
Figure 3-8 shows an AutoComplete text box using AutoSuggestAppend as the AutoCompleteMode and AllUrl as the AutoCompleteSource.
Figure 3-8. An AutoComplete text box
The TextBox and ComboBox controls both provide the same functionality. If you use AutoSuggest or AutoSuggestAppend with a ComboBox, the list of matches is displayed in a list under the control. However, this list shouldn't be confused with the list of entries that you've added to the ComboBox.Items property. When you click the drop-down arrow for the ComboBox, you'll see your list of items, not the list of AutoComplete suggestions. Both lists are completely separate, and there is no programmatic way for you to interact with the AutoComplete list. The only exception is if you create a ComboBox with an AutoCompleteSource of CustomSource or ListItems.
3.6.2. What about...
...using AutoComplete in other controls? Unfortunately, there's no managed way to do it in .NET. However, you can retrieve the information you need directly from the registry. For example, if you look in the Software\Microsoft\Internet Explorer\TypedURLs section of the HKEY_CURRENT_USER registry key, you'll find the list of recently typed in URLs. To retrieve these items programmatically, refer to classes like the RegistryKey in the Microsoft.Win32 namespace.