Introduction
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
class CComboCompletion : public CComboBox
{
public:
CComboCompletion();
public:
public:
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
public:
virtual ~CComboCompletion();
BOOL m_bAutoComplete;
protected:
afx_msg void OnEditUpdate();
DECLARE_MESSAGE_MAP()
};
#endif
#include "stdafx.h"
#include "ComboCompletion.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CComboCompletion::CComboCompletion()
{
m_bAutoComplete = TRUE;
}
CComboCompletion::~CComboCompletion()
{
}
BEGIN_MESSAGE_MAP(CComboCompletion, CComboBox)
ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditUpdate)
END_MESSAGE_MAP()
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;
}
return CComboBox::PreTranslateMessage(pMsg);
}
void CComboCompletion::OnEditUpdate()
{
if (!m_bAutoComplete)
return;
CString str;
GetWindowText(str);
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)
SetEditSel(dStart, dEnd);
else
SetEditSel(nLength, -1);
}
Chris Maunder is the co-founder of
CodeProject and
ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.
In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project,
CodeProject.AI.
In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.