Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Seek and Ye Shall Find: AutoComplete in Windows Forms 2.0

New properties simplify creating autocomplete list and combo boxes in .NET 2.0.


advertisement
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 keng@mcwtech.com. I'll publish interesting submissions right here.

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 http://www.google.com/webhp?complete=1&hl=en 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.

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:\Windows\System32, 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.

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.



   
Ken Getz is a senior consultant with MCW Technologies and splits his time between programming, writing, and training. He specializes in tools and applications written in Visual Studio .NET and Visual Basic. Ken is the author of the highly rated .Finalize() column in CoDe Magazine. He is also the co-author of several best-selling books, including Access 2002 Developer's Handbooks with Paul Litwin and Mike Gunderloy, Visual Basic Language Developer's Handbook with Mike Gilbert, and VBA Developer's Handbook with Mike Gilbert (Sybex). He co-wrote several training courses for Application Developer's Training Company, including VB.NET, ASP.NET, Access 2000 and 97, Visual Basic 6, and Visual Basic 5 seminars. He has also recorded video training for AppDev covering VB.NET, ASP.NET, VB6, Access 2000, and Access 97. Ken is a frequent speaker at technical conferences and has spoken often at Microsoft's Tech-Ed conference. Ken's also a technical editor for Access-VB-SQL Advisor magazine and a columnist for Informant Publications' asp.netPRO magazine..
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap