Click here to Skip to main content
Click here to Skip to main content

Auto-complete edit and combo, without Platform SDK

, 26 Feb 2002
Rate this:
Please Sign up or sign in to vote.
How to enable file system and URL auto-complete for edit controls and combo-boxes, without downloading the entire Platform SDK


Here's a quick article that shows how you can add the handy auto-completion effect that you see in the Win2K file-open dialog for your own edits and combo boxes - without having to download the entire Platform SDK (9 hours on a 56K modem!). If you already have the Platform SDK, this article is probably useless to you.


I'm not providing any demo project here, the code is small enough that you can just copy and paste (one function!).

Some constants

These would be defined in the Platform SDK, but since we're not using that, I'll include these here. You'll have to copy these to your app.

// See the MSDN for SHAutoComplete for a full 
// description of what these options do.
#define SHACF_DEFAULT                   0x00000000
#define SHACF_FILESYSTEM                0x00000001
#define SHACF_URLHISTORY                0x00000002
#define SHACF_URLMRU                    0x00000004
#define SHACF_USETAB                    0x00000008
#define SHACF_FILESYS_ONLY              0x00000010

#define SHACF_AUTOSUGGEST_FORCE_ON      0x10000000
#define SHACF_AUTOSUGGEST_FORCE_OFF     0x20000000
#define SHACF_AUTOAPPEND_FORCE_ON       0x40000000
#define SHACF_AUTOAPPEND_FORCE_OFF      0x80000000

The simple wrapper function

    (HWND hTarget, DWORD dwFlags);

bool SetupForAutoComplete(HWND hTarget, DWORD dwFlags)
    if (hTarget == NULL)
        return false;

    bool ok = false;

    if (hSHLWAPIDLL == NULL)
        hSHLWAPIDLL= LoadLibrary("SHLWAPI.DLL");
        if (hSHLWAPIDLL== NULL)
            return false;


    if (pSHAC != NULL)
        ok = SUCCEEDED(pSHAC(hTarget, dwFlags));

    return ok;

Using it

First, make sure you've called ::CoInitialize(NULL); in your CWinApp's InitInstance. Without this, the call to SHAutoComplete will fail. You also need to call CoUninitialize(); in your CWinApp's ExitInstance. This initializes COM for your app.

I'd also recommend that you call FreeLibrary(hSHLWAPIDLL) in your CWinApp::ExitInstance(), just to be nice to the OS.

Now the good stuff

If you have an an edit control in your dialog class that's hooked up to a member variable, all you have to do is:

BOOL CMyDialog::OnInitDialog()
    // enable auto complete for m_myEdit

And that's it. Now, when you start typing a path, the edit control will drop down a list box with paths that match what you've typed so far - just like the file open dialog.

If you want to do this to a combo box, it's slightly more complicated:

BOOL CMyDialog::OnInitDialog()
    // the edit control of a CComboBox is always at dlg ID 1001
    CEdit * pMyEdit = (CEdit *)m_myCombo.GetDlgItem(1001);
    if (pMyEdit)
        // enable auto complete for m_myCombo
        SetupForAutoComplete(pMyEdit->m_hWnd, SHACF_FILESYSTEM);

And that's that.


The heart of this whole thing is SHAutoComplete. This is a function that lives in Shlwapi.DLL and is only available on Win2K or on machines with IE 5.0 or higher installed. But, you can safely call my SetupForAutoComplete function on machines that don't meet those requirements because the function checks for the DLL and then for the particular function. If neither are there, the function leaves gracefully.

More info

SHAutoComplete can do more than file system auto-complete. It can also do URL auto-complete (like the navigation bar in IE), and it can do it all with various other fun options. See the MSDN for SHAutoComplete for a full description of what these options are.

And that's that. Enjoy responsibly. Oh yeah, thanks Michael Dunn, for your invaluable input Smile | :)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chris Losinger
Software Developer
United States United States
Chris Losinger is the president of Smaller Animals Software, Inc..

Comments and Discussions

GeneralI do just like that, but unuseful!! Pinmemberfdmakk2-Feb-05 16:44 
Generalcombo box PinsussAnonymous10-Jul-04 22:46 
GeneralRe: combo box Pinmembertoxcct4-Dec-04 4:32 
Questionhow can enter key close the drop down box? PinsussDanny Neijns1285-Jul-04 1:28 
Hi All,
the drop down box with all the files/url's can be closed by pressing the ESC key, can this also be achieved by using the "enter" key?
GeneralWith Platform SDK PinmemberAnakin_Skaywalker17-Jan-04 13:43 
GeneralHelp! Locks program Pinmemberblindcop8-Jan-04 8:12 
GeneralRe: Help! Locks program Pinmemberblindcop9-Jan-04 5:25 
GeneralVC++ using edit box and dialog PinsussHaider Chy5-Jan-03 21:59 
GeneralRe: VC++ using edit box and dialog PinmemberChris Losinger6-Jan-03 15:54 
QuestionSHACF_FILESYSTEM ? Pinmemberme27-Mar-02 15:24 
AnswerRe: SHACF_FILESYSTEM ? PinmemberChris Losinger27-Mar-02 15:52 
Questionhow to make the Edit on the htmlpages has the auto-completion effect? Pinmemberbenben8-Mar-02 1:41 
QuestionHow to add strings in auto complete droplist PinmemberAnonymous1-Mar-02 4:25 
Generali was wondering how that worked... PinmemberShog927-Feb-02 6:40 
GeneralRe: i was wondering how that worked... PinmemberChris Losinger27-Feb-02 6:44 
GeneralProblems PinmemberDerek Price27-Feb-02 3:14 
GeneralRe: Problems PinmemberChris Losinger27-Feb-02 4:05 
GeneralRe: Problems PinmemberChris Losinger27-Feb-02 4:06 
GeneralRe: Problems PinmemberDerek Price27-Feb-02 4:29 
GeneralRe: Problems PinmemberMagnus H27-Feb-02 4:07 
GeneralGreat stuff! PinmemberJörgen Sigvardsson26-Feb-02 23:01 
GeneralMore optimization PinmemberRavi Bhavnani27-Feb-02 3:31 
GeneralRe: More optimization PinmemberJörgen Sigvardsson27-Feb-02 3:46 
GeneralRe: More optimization PinmemberAnonymous27-Feb-02 4:46 
GeneralAlmost there... PinmemberChris Losinger27-Feb-02 4:02 
GeneralRe: Almost there... PinmemberMagnus H27-Feb-02 4:12 
GeneralRe: Almost there... PinmemberChris Losinger27-Feb-02 4:15 
GeneralRe: Almost there... PinmemberMagnus H27-Feb-02 4:17 
GeneralRe: Almost there... PinmemberChris Losinger27-Feb-02 4:25 
GeneralRe: No need PinmemberTim Smith27-Feb-02 13:42 
GeneralRe: In fact... PinmemberTim Smith27-Feb-02 13:48 
GeneralRe: In fact... PinmemberChris Losinger27-Feb-02 14:25 
GeneralRe: In fact... PinmemberTim Smith27-Feb-02 15:47 
GeneralRe: In fact... PinmemberChris Losinger27-Feb-02 16:44 
GeneralRe: In fact... PinmemberTim Smith28-Feb-02 3:08 
GeneralRe: In fact... PinmemberChris Losinger28-Feb-02 3:29 
GeneralRe: In fact... PinmemberTim Smith28-Feb-02 5:29 
Generalfreelibrary PinmemberChris Losinger28-Feb-02 6:01 
GeneralRe: freelibrary PinmemberTim Smith28-Feb-02 6:31 
GeneralRe: No need PinmemberChris Losinger27-Feb-02 14:24 
GeneralRe: No need PinmemberTim Smith27-Feb-02 15:14 
GeneralAWESOME! PinmemberRomanNY26-Feb-02 19:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 27 Feb 2002
Article Copyright 2002 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid