Adding AutoComplete to your edit controls
For whatever reason, most of the toy applications I tend to write seem to end up being dialog based applications. I'm not 100% sure why, it probably has to do with the fact that the dialog box editor makes it really easy to drop controls on a page.
One of the things I often have to do is to specify a filename to the toy application. To do that, I usually add an edit control to display the filename and a "Browse..." button which brings up the common file browser control. Many people find it awkward to use the common file browser to locate files, so they often try typing into the edit control.
One of the really easy "nice touches" that you can specify to help with navigation is to add autocompletion support to the edit control. Fortunately, the Windows shell provides a really handy mechanism to do this in the SHAutoComplete function.
Adding autocomplete support is literally as easy as adding:
SHAutoComplete(hWndEdit, SHACF_FILESYSTEM);\
to your code.
It turns out that the shell has a fairly extensive autocomplete mechanism that allows you to customize this facility to a great deal (you can provide your own lists of elements, merge lists from multiple sources, etc).
For me, I almost always end up falling back to the old standby of SHAutoComplete - it's trivial to add it, and it adds a nice touch to your applications.
Comments
Anonymous
November 01, 2007
The comment has been removedAnonymous
November 01, 2007
Is this function also available for .NET Windows Forms?Anonymous
November 01, 2007
The comment has been removedAnonymous
November 01, 2007
The comment has been removedAnonymous
November 01, 2007
Thanks again, Larry. I guess I assumed it was something you needed to do repeatedly. Oops, my bad. I'm sure I can find a TON of uses for this in my applications. I especially like the URL modes. Cool!Anonymous
November 01, 2007
The comment has been removedAnonymous
November 01, 2007
The comment has been removedAnonymous
November 01, 2007
Gotcha. Thanks Larry. The internet is a wonderful world of misinformation. Sorry to thread-jack... -gAnonymous
November 01, 2007
Hugo : In .Net 2.0 System.Windows.Forms.TextBox has the AutoCompleteSource property. It is an enumeration. http://msdn2.microsoft.com/en-us/library/system.windows.forms.autocompletesource.aspxAnonymous
November 01, 2007
Hugo: In .NET 2.0 TextBox and ComboBox controls have AutoComplete properties that you can set to get this behavior. In prior versions you can use P/Invoke and the Handle property of the control to call SHAutoComplete directly.Anonymous
November 01, 2007
That is a handy trick. I'll have to keep it in mind, though I'll have to check if CE/Mobile supports it, I write plenty of code on that as well.Anonymous
November 01, 2007
The comment has been removedAnonymous
November 01, 2007
Jeff appears to almost have a clue. It's the gap that scares me.Anonymous
November 01, 2007
Sorry to be a bit rude, but I am completely lost. How can a post about auto complete turn into comparison of Windows Vista with Leopard?Anonymous
November 01, 2007
This is a great tip! As some one who uses a Tablet PC at various times I'm a big fan of AutoComplete. Thinking if its that easy it should have been an option in the OpenFileName struct used by GetOpenFileName(). BillAnonymous
November 02, 2007
The comment has been removedAnonymous
November 02, 2007
By the way, SHAutoComplete only works fine if the AutoComplete option for web addresses in the Internet Properties dialog is checked. If this option is unchecked, SHAutoComplete still returns S_OK, but no list is displayed.Anonymous
November 02, 2007
http://msdn2.microsoft.com/en-us/library/bb759862.aspx Please read the description about dwFlags, especially the first four. However, this MSDN topic does not negate Sven's argument that autocomplete from IE's dialog must be enabled for it to work in shell.Anonymous
November 05, 2007
After I posted my article on the SHAutoComplete , I mentioned it to one of my co-workers. His responseAnonymous
November 07, 2007
Great thing is that (probably due to custom subclassing) it doesn't work in MFC based apps.Anonymous
November 07, 2007
Igor: Are you sure? I know I've used that in MFC app's I've written in the past. If it's true, it stinks.Anonymous
November 08, 2007
Igor, it works fine for me. Were you trying with an Edit, or a ComboBoxEx? (If it was the combobox, you probably made the same mistake I did, missing the note about having to first get the combobox's Edit control, and then passing that handle to SHAutoComplete().) Larry, Great post! This one somehow snuck by me; I've been working with the other method all this time. Thanks for the great time and space saving tip.Anonymous
November 10, 2007
I agree with Jeff. More often than not, I encounter stuff Microsoft has done that seems to be there only for the sake of complexity. And about those cheepasses with NT4 drivers for their application - either they stay on NT4 or pay some programmers to develop them a new program - now the whole world suffers instead! (See, I optimized my comments and put two posts into one). :)Anonymous
November 14, 2007
The comment has been removedAnonymous
November 14, 2007
Oops, sorry. I misinterpreted what Larry said. He said if it is true.