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 (25)

Cancel reply

  1. Anonymous says:

    Whatever happened to Monaco?  The other day, my friend was enthusiastically showing me Leopard.  After a while, it struck me that Leopard isn’t kicking Vista’s ass, but iLife is.  iPhoto is nice, but I’m happy with Picasa2.  iMovie is great, but at least Vista Home comes with something like that.  But, GarageBand is very very compelling.  It fits in nicely with Apple’s iMac, iPod, iTunes music oriented force.  The message is clear: if you’re a serious musician, music hobbyist , or music enthusiast buy a Mac.

    There have been numerous rumors around the web that Microsoft had a GB like app (Monaco?) in the works for Vista dating back to 2004.  But, Vista is seriously lacking in this department.  What happened?


  2. Anonymous says:

    Is this function also available for .NET Windows Forms?

  3. Anonymous says:

    Thanks Larry!!!

    Just curious, under what handler for the edit box do you add the call to SHAutoComplete?  The docs say "SHAutoComplete should not be called more than once with the same HWND. Doing so results in a memory leak."  So, it seems that this may be kind of tricky to add it to the EN_CHANGE handler, unless I’m reading too much into the docs.  

  4. Hugo, I’m not sure…

    Coleman: I just add it in the WM_CREATE handler for the dialog which contains the edit control.

    Greg: Monaco?  What’s that?

  5. Anonymous says:

    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!

  6. Anonymous says:

    Larry, re: Monaco.  Here is the web site that put Garage Band, "Monaco" and your name together for me.

    If the site is wrong, so be it.  If you just can’t talk about it, I understand.



  7. I love how people take unsubstantiated rumors, and combine them with true stuff (we did revamp the audio system and multimedia visuals in Vista) and come up with new stuff.

    In all honesty, I have no idea what this "Monaco" thing is.

  8. Anonymous says:

    Gotcha.  Thanks Larry.  The internet is a wonderful world of misinformation.

    Sorry to thread-jack…


  9. Anonymous says:

    Hugo : In .Net 2.0

    System.Windows.Forms.TextBox has the AutoCompleteSource property. It is an enumeration.

  10. Anonymous says:


    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.

  11. Anonymous says:

    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.

  12. Anonymous says:

    This is a request to restart the article:

    Open Source and Hot Rods

    Here is my comment:

    Everyone here seems to have missed the key point.  The choices are generally Open Source versus a Microsoft development environment.  For simplicity, a user interface could be HTML, CSS and Javascript for either choice.  But on the Open Source side you primarily have a language choice and many folks choose PHP rather than Python or Perl.  On the Microsoft side you have fairly finite programming choices as well.  So where is the problem?

    Its the glue required on the Microsoft side both internal and external to any program.  Internally, some people have even expressed the feeling that Microsoft folks are paying some attention to PHP Object-Oriented Development simply because it raises the complexity, and therefore lowers the understanding, of Open Source development.

    Externally, you could write a book, and many have, that tries to explain and simplify the various ASPX gotchas.  Inherits, Codebehind, codefile, src, master, page, namespaces, etc., etc. The list goes on and on.  Even specification of 1.1 versus 2.0 on the .NET Framework can lead to errors if 2.0 has already been installed but remnants of 1.1 remain for some unexplained reason.

    And, that leads to a worse situation.  Either you hire someone that knows the ins and outs of the Microsoft software or you have your staff learn the Microsoft software.

    Meanwhile my Open Source PHP developers have written more applications for my business.  My Microsoft developers have the answers and are technically more knowledgeable.  However, they cost me money while my PHP developers introduce new revenue streams far faster.

    Unless it is a government agency, a tech-oriented company, or a company that bought into the old "buy blue" and you can’t be fired (or is that "green" for Microsoft), the Open Source world seems to be eating the Microsoft lunch.  I think that will only accelerate.  

  13. Anonymous says:

    Jeff appears to almost have a clue. It’s the gap that scares me.

  14. Tanveer Badar says:

    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?

  15. Anonymous says:

    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().


  16. Anonymous says:

    Is Jeff related to aManFromMars?

    (And many others.)

  17. Anonymous says:

    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.

  18. Tanveer Badar says:

    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.

  19. Anonymous says:

    After I posted my article on the SHAutoComplete , I mentioned it to one of my co-workers. His response

  20. Anonymous says:

    Great thing is that (probably due to custom subclassing) it doesn’t work in MFC based apps.

  21. 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.

  22. Anonymous says:

    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.

  23. Anonymous says:

    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). 🙂

  24. Anonymous says:

    I tried on an edit box, Visual Studio 2005 (that’ll be MFC 8.0 right?). I did it in OnInitDialog().

    As Larry said in a reply to my post, it doesn’t work for him either. Now if someone in Microsoft could tell us why and how to fix it… That would be cool.

    If you think about it — it is totally unintuitive. You get to use such a great thing if you write pure Win32 code (which many believe it is hard to do) and you don’t get it if you use MFC which was made to make your life easier.

  25. Anonymous says:

    Oops, sorry. I misinterpreted what Larry said. He said _if_ it is true.

Skip to main content