The Combo Box: Automatic List Creation


Using an External List


In the next previous sections, we saw how to create a list of items. The .NET Framework provides an alternative. Instead of creating a list from scratch, you can use one that exists already. For example, you can use a list of recently accessed web sites or custom list of your own. To assist you with this, the ComboBox class provides with three techniques.

To specify an external list of items to use for the combo box, you have two options. You can use the AutoCompleteSource property, that is based on the AutoCompleteSource enumeration. The members of this enumeration are: None, RecentlyUsedList, FileSystem, FileSystemDirectories, HistoryList, ListItems, AllSystemSources, AllUrl, and CustomSource. Imagine that you want to use the list of web pages you had visited lately. To use that list, you can specify the AutoCompleteSource as HistoryList.

After specifying the source of the list, use the AutoCompleteMode property to specify how the combo box (or rather the text box side of the control) will assist the user. This property is based on the AutoCompleteMode enumeration that has four members. None is the default value. Imagine you had set the value of the AutoCompleteSource property as HistoryList. If you specify AutoCompleteMode as:

  • Suggest: In the text box part of the combo box, the user can click and start typing. A list of closely-matched items would display:

    In this case, as soon as the user types h, a list of URLs that start with h (for http) would come up. Once the user sees the desired item, he or she can then click that item to select it. Since there are many items, to continuously narrow the list, the user can keep typing until the desired item comes up
  • Append: In the text box part, the user can start typing. The control would then start looking for the closest matches and try to complete the user's entry with those available. Here is an example:

    First the user types h and http:// comes up as the first closest match. Then, the user specifies that the address starts with m and the compiler suggests, in alphabetical order, the closest URL with that. Then, the user types ms and finds out that msdn2 is available

  • SuggestAppend: This is a combination of the previous two options. When the control comes up, the user can start typing. The control would then display the list of items that start with what the user typed and it would display the starting closest match

    The user can continue typing. If the desired item appears in the list, the user can select it. Otherwise, as the user is typing, the closest match displays in the text box part of the control

    Combo Box Combo Box

Using a Custom List


Instead of using an external list, you can create your own. To do this, use the AutoCompleteCustomSource property. At design time, to create a list of strings, access the Properties window for the text box. In the Properties window, click the ellipsis button of the AutoCompleteCustomSource field to open the String Collection Editor. Enter the strings separated by a hard Return, and click OK. You can also programmatically create the list. To assist you, the .NET Framework provides a class named AutoCompleteStringCollection. The AutoCompleteStringCollection class implements the IList, the ICollection, and the IEnumerable interfaces.

After creating the custom list, to let the combo box use it, set the AutoCompleteMode property to CustomSource.


Previous Copyright © 2007-2013, FunctionX Home