Auto-completion ComboBox
Download sample project and source files
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_COMBOBOXEX_H__115F422E_5CD5_11D1_ABBA_00A0243D1382__INCLUDED_)
#define AFX_COMBOBOXEX_H__115F422E_5CD5_11D1_ABBA_00A0243D1382__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// ComboBoxEx.h : header file
//
// Copyright (c) Chris Maunder 1997.
// Please feel free to use and distribute.
/////////////////////////////////////////////////////////////////////////////
// CComboBoxEx window
class CComboBoxEx : public CComboBox
{
// Construction
public:
CComboBoxEx();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CComboBoxEx)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CComboBoxEx();
BOOL m_bAutoComplete;
// Generated message map functions
protected:
//{{AFX_MSG(CComboBoxEx)
afx_msg void OnEditUpdate();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_COMBOBOXEX_H__115F422E_5CD5_11D1_ABBA_00A0243D1382__INCLUDED_)
// ComboBoxEx.cpp : implementation file
//
// Copyright (c) Chris Maunder 1997.
// Please feel free to use and distribute.
#include "stdafx.h"
#include "ComboBoxEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CComboBoxEx
CComboBoxEx::CComboBoxEx()
{
m_bAutoComplete = TRUE;
}
CComboBoxEx::~CComboBoxEx()
{
}
BEGIN_MESSAGE_MAP(CComboBoxEx, CComboBox)
//{{AFX_MSG_MAP(CComboBoxEx)
ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditUpdate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComboBoxEx message handlers
BOOL CComboBoxEx::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);
}
// if we are not to auto update the text, get outta here
if (!m_bAutoComplete)
return;
// Get the text in the edit box
CString str;
GetWindowText(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);
else
SetEditSel(nLength, -1);
Last updated: 12 Sep 1998

Comments
How we can add an string to list box item of combobox by code?
Posted by Legacy on 05/01/2003 12:00amOriginally posted by: zeinalzadeh
How we can add an string to list box item of combobox by code.While I exit from program that string value be saved on list box data of combobox?
ReplyHow to capture the event from Popup menu from the right click??
Posted by Legacy on 02/06/2003 12:00amOriginally posted by: R.Singaravelan
I need to know how to get the key pressed event from the Popup menu(On right click) of the Combo Box(Dropdown list)
ReplyFix for ShowDropDown() eating up the first entered character
Posted by Legacy on 02/27/2002 12:00amOriginally posted by: Manish Singh
ReplyProblem on using this code
Posted by Legacy on 02/26/2002 12:00amOriginally posted by: Meghana Parwate
Hello,
ReplyI have a dialog-based application . The dialog has a combo box and two buttons - "OK" and "Cancel".
I used this code to add the autocompletion feature.
But now the problem is :
When I press enter after typing a string or on autocompletion , the event that should be generated when the Ok button is clicked , does not occur. In other words,
if I press enter , it does not execute the code that is supposed to be executed if the Ok button would have been clicked. Ok button clicked code works properly if the Ok button is pressed explicitly.
Does anyone know why this happens?
Meghana
Extended with prefix matching (like "http://")
Posted by Legacy on 02/11/2002 12:00amOriginally posted by: Michael Christensen
ReplyExcellent Code! Works like a charm!
Posted by Legacy on 11/07/2001 12:00amOriginally posted by: Owen Matthews
Excellent Code! Works like a charm!
ReplyCool code! CComboEx exists, also editing mid-text not selecting
Posted by Legacy on 08/31/2001 12:00amOriginally posted by: Tony Hwang
Great code!
Just two comments:
1. CComboBoxEx exists in VC6.0, I renamed it to CComboBoxEx2 and all is fine.
2. If you type in for example, "oranges", and then delete the "s" and the "an", and then type back in the "an", it autocompletes and fills in the "s", but no text is selected. This might look inconsistent with "normal" behavior?
Anyways, I modified the code to fix #2. Thanks!
- Tony
ReplyHow about this?
Posted by Legacy on 06/11/2001 12:00amOriginally posted by: JeongHwan Cho
Thanks Maunder, for his original code!
But, I found some minor flaws in Maunder's code
in implementing in my program.
They are;
1. If ListBox is dropped down, ComboBox item is selected
and deselected very short time every time i type a
character. So it looks like flickering.
2. If some text is already exists in the EditBox
portion of the ComboBox, auto completion does not
work.
So, I applied some modification on Maunder's code.
void CComboBoxEx::OnEditupdate()
{
// TODO: Add your control notification handler code here
if(m_bAutoComplete)
{
CString str, buf;
DWORD dwCurSel;
WORD nCurPos;
int nIndex;
GetWindowText(str);
dwCurSel = GetEditSel();
nCurPos = HIWORD(dwCurSel);
buf = str.Left(nCurPos);
nIndex = FindString(-1, buf);
if(nIndex != CB_ERR)
{
GetLBText(nIndex, buf);
buf = buf.Mid(nCurPos);
str.Insert(nCurPos, buf);
SetWindowText(str);
SetEditSel(nCurPos, nCurPos + buf.GetLength());
}
}
}
Good coding!
ReplyThe Quest for a better Combo Box
Posted by Legacy on 02/07/2001 12:00amOriginally posted by: Mr. P
Looking to use/implement a ComboBox that does Type-ahead and Drops the list down, highlighting the "best guess" (if you will).
Apparently there is something Hoekee going on when I try to ShowDropDown(TRUE), cuz it eats the first letter of the actually typed string (but shows displays the character as having been matched).
I have/am trying to catch WM_KILLFOCUS (or something) in OnKillFocus(), and I tried to ---don't laugh--- set a timer cuz there are so many Messages that are being sent...
If I Do <::Crossing my fingers::> devise a solution, I will post it :)
Good Times,
ReplyMr. P
In Dialogbar ?
Posted by Legacy on 01/27/2001 12:00amOriginally posted by: Bryce Burrows
Hi
sorry if this is a stupid question
but this would be great in a dialogbar for an MDi or SDI app
how does one tie it in to the view or frame ?
Regards
Bryce
ReplyLoading, Please Wait ...