Section 3.3. Create Auto-Complete Text Boxes


3.3. Create Auto-Complete Text Boxes

When you type a URL into Internet Explorer, it attempts to help you complete the address by providing a list of addresses that match what you've typed so far. The newly updated TextBox and ComboBox controls provided with Visual Studio 2005 allow you to add that functionality to your Windows Forms as well.

3.3.1. How do I do that?

Create a new Windows application, call it TextComplete, and drag a TextBox onto the form.

Click the TextBox control, and in the properties window drop down the AutoCompleteMode property, as shown in Figure 3-15. (I blanked out the surrounding properties to make this easier to see.)

The three choices for AutoCompleteMode are shown in Table 3-4.

Table 3-4. AutoCompleteMode choices

Mode

Meaning

Suggest

Displays the drop-down list populated with one or more suggested completion strings

Append

Appends the remainder of the most likely candidate string (highlighted)

SuggestAppend

Combines both Suggest and Append


For AutoCompleteMode to work, you must specify where to get the list of suggestions. You do so in the AutoCompleteSource property, as shown in Figure 3-16 (again, to make the figure easier to read, I've blanked out the other properties).

Figure 3-15. The AutoCompleteMode property


Figure 3-16. The AutoCompleteSource property


The possible values and what they indicate are listed in Table 3-5.

Table 3-5. AutoCompleteSource choices

Source

Meaning

FileSystem

Recently entered file paths.

HistoryList

Taken from Internet Explorer's history list.

RecentlyUsedList

All documents in the "most recently used list" in the Start menu.

AllURL

All sites recently visited.

AllSystemSources

All URLs and all files.

CustomSources

Items in the AutoCompleteCustomSource collection that you populate either at design time or programmatically.

ListItems

Not valid with TextBox. Used with ComboBox to pick from items in the ComboBox Items collection.


Once you've filled in these properties, the list works automatically for you, as shown in Figure 3-17.

Figure 3-17. AutoCompleteMode at work


3.3.2. What just happened?

For the auto-complete text box in Figure 3-17, I set AutoCompleteMode to Suggest and I set AutoCompleteSource to AllUrl. You might want to try changing these settings and seeing the resulting changes in the text box.

3.3.3. What about . . .

...the word "custom" in the auto-complete enumeration? How do I create a custom source for auto-completion?

The auto-complete source must be an AutoCompleteStringCollection. You can easily create a custom source at design time by setting the AutoCompleteSource property to CustomSource and then clicking the AutoCompleteCustomSource property to fill in the string collection with whatever strings you want to appear in the drop-down list. Of course, you can set this programmatically at runtime as well.

3.3.4. Where can I learn more?

You can learn a bit more about the properties associated with auto-completion by checking the Help Index entry for the TextBox class.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net