Click here to Skip to main content
11,927,378 members (51,729 online)
Click here to Skip to main content
Add your own
alternative version


94 bookmarked

Implementing an autocompleting Combobox

, 18 Oct 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
A combobox that autocompletes as you type



I had a need for a combobox that would auto-complete, very much like the URL edit box in the toolbar of Netscape Navigator. It was actually surprisingly simple since the base CComboBox is so rich in functionality.

The basic idea is that every time the text in the edit box changes, check to see if there is any text in the drop down list that is prefixed by this edit box text. Handle the CBN_EDITUPDATE message to get the text change notifications, and use GetWindowText() to get the text. CComboBox::SelectString will look for a string in the list which is prefixed by the given string, and select it into the edit box. I then select the portion of text that was added to the users typed text so that they can continue typing and have the additions ignored if they wish. That takes care of 90% of the work.

The only trick is in handling backspaces and deletes. When a user hits delete, the text is changed, and the auto-completion routine will try to restore that text back again. Just check in PreTranslateMessage for a KEY_DOWN message with a virtual key of VK_DELETE or VK_BACK, and temporarily disable the auto-complete mechanism for those key strokes.

Source code

#if !defined(AFX_ComboCompletion_H__115F422E_5CD5_11D1_ABBA_00A02__INCLUDED_)
#define AFX_ComboCompletion_H__115F422E_5CD5_11D1_ABBA_00A02__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

// ComboCompletion.h : header file
// Copyright (c) Chris Maunder 1997.
// Please feel free to use and distribute.

// CComboCompletion window

class CComboCompletion : public CComboBox
// Construction

// Attributes

// Operations

// Overrides
    // ClassWizard generated virtual function overrides
    virtual BOOL PreTranslateMessage(MSG* pMsg);

// Implementation
    virtual ~CComboCompletion();

    BOOL m_bAutoComplete;

    // Generated message map functions
    afx_msg void OnEditUpdate();



// Microsoft Developer Studio will insert additional declarations immediately 
// before the previous line.


// ComboCompletion.cpp : implementation file
// Copyright (c) Chris Maunder 1997.
// Please feel free to use and distribute.

#include <span class="code-string">"stdafx.h"</span>
#include <span class="code-string">"ComboCompletion.h"</span>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

// CComboCompletion

    m_bAutoComplete = TRUE;


BEGIN_MESSAGE_MAP(CComboCompletion, CComboBox)

// CComboCompletion message handlers

BOOL CComboCompletion::PreTranslateMessage(MSG* pMsg)
    // Need to check for backspace/delete. These will modify the text in
    // the edit box, causing the auto complete to just add back the text
    // the user has just tried to delete. 

    if (pMsg->message == WM_KEYDOWN)
        m_bAutoComplete = TRUE;

        int nVirtKey = (int) pMsg->wParam;
        if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
            m_bAutoComplete = FALSE;

    return CComboBox::PreTranslateMessage(pMsg);

void CComboCompletion::OnEditUpdate() 
  // if we are not to auto update the text, get outta here
  if (!m_bAutoComplete) 

  // Get the text in the edit box
  CString str;
  int nLength = str.GetLength();
  // Currently selected range
  DWORD dwCurSel = GetEditSel();
  WORD dStart = LOWORD(dwCurSel);
  WORD dEnd   = HIWORD(dwCurSel);

  // Search for, and select in, and string in the combo box that is prefixed
  // by the text in the edit box
  if (SelectString(-1, str) == CB_ERR)
      SetWindowText(str);            // No text selected, so restore what 
                                     // was there before
      if (dwCurSel != CB_ERR)
        SetEditSel(dStart, dEnd);    //restore cursor postion

  // Set the text selection as the additional text that we have added
  if (dEnd < nLength && dwCurSel != CB_ERR)
      SetEditSel(dStart, dEnd);
      SetEditSel(nLength, -1);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Comments and Discussions

GeneralCComboCOmpletion in two dialogs Pin
Anu_Bala27-Jul-08 20:25
memberAnu_Bala27-Jul-08 20:25 
GeneralLittle Bug Pin
chris17523-Aug-06 0:56
memberchris17523-Aug-06 0:56 
GeneralRe: Little Bug Pin
ellolazo28-Aug-06 10:27
memberellolazo28-Aug-06 10:27 
GeneralRe: Little Bug Pin
ellolazo31-Aug-06 6:34
memberellolazo31-Aug-06 6:34 
GeneralEnter in combobox and default button Pin
ellolazo12-Jun-06 7:57
memberellolazo12-Jun-06 7:57 
GeneralHelp..Enter key Pin
G.Inbakumar20-Feb-06 6:29
memberG.Inbakumar20-Feb-06 6:29 
QuestionGreat Works,Can you complete it with C#? Pin
kv40008-Jun-05 15:59
memberkv40008-Jun-05 15:59 
AnswerRe: Great Works,Can you complete it with C#? Pin
Bob-ish27-Aug-05 12:51
memberBob-ish27-Aug-05 12:51 
GeneralSuggestion Pin
starschen20-Apr-05 18:48
sussstarschen20-Apr-05 18:48 
GeneralBackspace and Enter key Pin
rlaley28-Oct-04 2:54
memberrlaley28-Oct-04 2:54 
GeneralRe: Backspace and Enter key Pin
rlaley31-Oct-04 17:21
memberrlaley31-Oct-04 17:21 
GeneralRe: Backspace and Enter key Pin
ReDFoX20-Feb-05 5:23
sussReDFoX20-Feb-05 5:23 
GeneralRe: Backspace and Enter key Pin
rlaley20-Feb-05 18:20
memberrlaley20-Feb-05 18:20 
GeneralCapturing a right-click on the combo box Pin
Flic13-Feb-03 18:07
memberFlic13-Feb-03 18:07 
GeneralRe: Capturing a right-click on the combo box Pin
abdulsoni18-Dec-03 21:29
memberabdulsoni18-Dec-03 21:29 
GeneralView options Pin
Anonymous9-May-02 5:16
memberAnonymous9-May-02 5:16 
GeneralSimple-type ComboBox Highlight Pin
Maxwell B.6-Jan-02 14:49
memberMaxwell B.6-Jan-02 14:49 
GeneralGetCurSel() for the CComboBox is returns 2 different values on consecutive calls Pin
Ed Mitchell3-Oct-01 4:23
memberEd Mitchell3-Oct-01 4:23 
Michael Martin13-Sep-01 4:25
memberMichael Martin13-Sep-01 4:25 
Carlos Antollini13-Sep-01 4:53
memberCarlos Antollini13-Sep-01 4:53 
GeneralGetCurSel() doesn't return the selected index. Pin
Kenneth Chen2-Aug-01 21:27
memberKenneth Chen2-Aug-01 21:27 
GeneralRe: GetCurSel() doesn't return the selected index. Pin
Kyle Poole17-May-03 8:03
memberKyle Poole17-May-03 8:03 
GeneralRe: GetCurSel() doesn't return the selected index. Pin
M. Wohlers3-Jan-05 2:43
memberM. Wohlers3-Jan-05 2:43 
GeneralUsing your Class in a non Dialog based app for example in a SDI/MDI embedded in toolbar & Rebar similar to IE 5 Pin
Sean25-Jan-01 4:03
memberSean25-Jan-01 4:03 
GeneralRe: Using your Class in a non Dialog based app for example in a SDI/MDI embedded in toolbar & Rebar similar to IE 5 Pin
Chris Sims18-Jul-01 10:34
memberChris Sims18-Jul-01 10:34 
GeneralCComboCompletion Pin
Larisa15-Nov-00 2:37
memberLarisa15-Nov-00 2:37 
GeneralRe: CComboCompletion Pin
DancnDude20-Dec-01 10:21
memberDancnDude20-Dec-01 10:21 
GeneralThanks... Pin
TEF23-Oct-00 15:43
sussTEF23-Oct-00 15:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web03 | 2.8.151126.1 | Last Updated 18 Oct 2000
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid