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_BACK, and temporarily disable the auto-complete mechanism for those key strokes.
#if _MSC_VER >= 1000
class CComboCompletion : public CComboBox
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnEditUpdate();
#define new DEBUG_NEW
static char THIS_FILE = __FILE__;
m_bAutoComplete = TRUE;
BOOL CComboCompletion::PreTranslateMessage(MSG* pMsg)
if (pMsg->message == WM_KEYDOWN)
m_bAutoComplete = TRUE;
int nVirtKey = (int) pMsg->wParam;
if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
m_bAutoComplete = FALSE;
int nLength = str.GetLength();
DWORD dwCurSel = GetEditSel();
WORD dStart = LOWORD(dwCurSel);
WORD dEnd = HIWORD(dwCurSel);
if (SelectString(-1, str) == CB_ERR)
SetWindowText(str); if (dwCurSel != CB_ERR)
SetEditSel(dStart, dEnd); }
if (dEnd < nLength && dwCurSel != CB_ERR)
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.