Larry Osterman's WebLog

Confessions of an Old Fogey
Blog - Title

Adding AutoComplete to your edit controls

Adding AutoComplete to your edit controls

  • Comments 25

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.

  • 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?


  • Is this function also available for .NET Windows Forms?

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

  • 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?

  • 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!

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



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

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

    Sorry to thread-jack...


  • Hugo : In .Net 2.0

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

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

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

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

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

  • 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?

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


Page 1 of 2 (25 items) 12