Seek and Ye Shall Find: AutoComplete in Windows Forms 2.0

Seek and Ye Shall Find: AutoComplete in Windows Forms 2.0

reetings from South Florida. You know what? It’s hot here in June. And it’s getting hotter. And did I mention the humidity? Hey, I grew up in Houston, so I know all about humid. I remember riding my bicycle to school in the 70s, and finding my hair all curled up in little wet ringlets by the time I got there. (You’ll have to use your imagination to see the ringlets, much less any hair at all, I’m afraid. Those of you who were there at the time remember, I’m sure.) Our new regime is to arise and get on the bicycle, and ride around this lovely island before sitting down to work. We live in the middle of a u-shaped island, and it’s three miles either way to the mainland. It’s five miles between the legs of the “u”, and the whole 11-mile trip takes around 45 minutes, more or less. One day, I lost two pounds in sweat during the ride. No kidding. Last weekend, I rode around 27 miles, and that took four pounds of water out of me. (Too bad it comes right back.)

So, to be honest, I’m not convinced we’re going to retire in South Florida. I’m taking all suggestions, at this point, about where we should end up for the next-to-last move. (I don’t really care much about the final move, to be honest. Put me anywhere.) I only have a few criteria to make the next locale a perfect one. I understand that it’s not possible to satisfy all these needs, but I’m hoping that with your help, I can come close. I’d take 80%, if I could get it. So what’s on the list? Moderate climate (four seasons, but no snow on the ground and no sweltering summers); five acres or more; 3000 square feet with no stairs; trees, yes, lots of trees (I miss looking out the windows at trees); near a medium-sized city (Sacramento, for example); within an hour of an airport served by major airlines, and within an hour of Costco (need I start on the Costco blueberries?); oh, a pool would be nice, too; not more then $500,000. I’m afraid I’m looking for a house at 1970’s prices, unfortunately, so I’m not thinking this will happen fast. But please, if you have ideas on the right neighborhood, send me e-mail at [email protected]. I’ll publish interesting submissions right here.

See also  Comparing different methods of testing your Infrastructure-as-Code

Speaking of searching, I wonder how many folks have written their own support in .NET Windows applications for searching within combo boxes? You know what I mean? I first encountered this with a certain non-Microsoft financial application, but lots of applications have absorbed the technology since then?even Google uses client-side script to get this functionality. (Visit to try this beta product out.) Both TextBox and ComboBox controls in lots of applications supply suggestions as you type, and it was a pain to accomplish this in previous versions of Microsoft’s development tools.

New support in TextBox and ComboBox controls in Visual Studio 2005 makes this functionality really easy, and it’s amazing how well the folks at Microsoft have implemented this functionality. If you investigate these controls, you’ll find three properties that manage this behavior: AutoCompleteMode, AutoCompleteSource, and AutoCompleteCustomSource. You can set the AutoCompleteMode property to one of the AutoCompleteMode enumerated values (Append, None, Suggest, SuggestAppend). None is the default, but assuming that you’ve supplied a list of values, Append simply appends the first item that matches what you’ve typed so far; Suggest provides a drop-down list with a list of values that match what you’ve typed, and SuggestAppend displays the list, and when you press Enter or Tab to move focus from the control, appends the selected value into the control’s contents.

The AutoCompleteSource property provides a large number of options for where you get the items in the list. You can choose from None (the default); FileSystem (uses the file system); FileSystemDirectories (uses only folders within the file system); HistoryList (uses your history list); or RecentlyUsedList (uses your most recently used URLs). In addition, you can select CustomSource, which allows you to specify a string collection in the AutoCompleteCustomSource property. ComboBox controls allow you to specify ListItems for the AutoCompleteSource property, which uses the items in the control’s list for its suggestion list. Finally, the AutoCompleteSource enumeration includes items that are the union of other members: AllUrl is the same as HistoryList and RecentlyUsedList combined. AllSystemSources is the same as AllUrl and FileSystem combined. Given these combinations, it’s easy to set up a TextBox or ComboBox so that users can select from a list of suggestions with minimal typing.

See also  Comparing different methods of testing your Infrastructure-as-Code

Here’s the simplest way to try out this functionality: Create a new Windows application and place a TextBox control on the form. Add the following code to the form’s Load event handler (you can set these properties at design time, as well, but it’s easier for me to describe the properties in code, given the limitations of this medium).

   textBox1.AutoCompleteMode =      AutoCompleteMode.SuggestAppend   textBox1.AutoCompleteSource =      AutoCompleteSource.FileSystem

That’s it! Run the project, and in the text box, enter C:WindowsSystem32, and watch what happens as you type. (This exercise is far more interesting if you actually have Windows installed on your C: drive, of course). Press Enter to select a particular file or folder, and that name appears in the text box. Pretty easy, right? Modify the code to try other AutoCompleteMode values, and then try other AutoCompleteModeSource values, as well. No kidding?this is a huge feature that every Windows developer has always wanted available without having to write code.

Displaying a custom list of suggestions isn’t any harder, really. (I do wish there was a way to bind the AutoCompleteCustomSource to some data source, but the property requires a string array.) Try modifying the code so that it looks like this.

   textBox1.AutoCompleteMode =       AutoCompleteMode.SuggestAppend;   textBox1.AutoCompleteSource =       AutoCompleteSource.CustomSource;   textBox1.AutoCompleteCustomSource.       AddRange(new string[] {       "California", "Texas", "Tennesee",       "Massachusetts", "Minnesota",       "Florida" });

Now, when you run the project and enter “M” into the text box, you can select one of the states that begins with “M”. Have fun selecting states?you’ll see that the text box operates just as you would expect.

ComboBox controls work much the same way, except that you can also have the AutoCompleteSource be set to ListItems, in which case the suggestions come from the drop-down list. Try the same exercise with a ComboBox control to see how that control behaves.

See also  Comparing different methods of testing your Infrastructure-as-Code

With the addition of these new properties, searching within ComboBox and TextBox controls has improved drastically in Visual Studio 2005. And remember, if you want to help out with Ken’s Search 2005, please send e-mail with your favorite places to retire. The winning suggestion gets a…well, we’ll think of something.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist