3.3. Create Auto-Complete Text BoxesWhen 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.
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 propertyFigure 3-16. The AutoCompleteSource propertyThe possible values and what they indicate are listed in Table 3-5.
Once you've filled in these properties, the list works automatically for you, as shown in Figure 3-17. Figure 3-17. AutoCompleteMode at work3.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. |