Click here to Skip to main content
Licence CPOL
First Posted 18 Oct 2000
Views 226,446
Downloads 2,394
Bookmarked 87 times

Implementing an autocompleting Combobox

By | 18 Oct 2000 | Article
A combobox that autocompletes as you type

ComboCompletion.gif

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

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


/////////////////////////////////////////////////////////////////////////////
// CComboCompletion window

class CComboCompletion : public CComboBox
{
// Construction
public:
    CComboCompletion();

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CComboCompletion)
    public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CComboCompletion();

    BOOL m_bAutoComplete;

    // Generated message map functions
protected:
    //{{AFX_MSG(CComboCompletion)
    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 



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

#include "stdafx.h"
#include "ComboCompletion.h"

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

/////////////////////////////////////////////////////////////////////////////
// CComboCompletion

CComboCompletion::CComboCompletion()
{
    m_bAutoComplete = TRUE;
}

CComboCompletion::~CComboCompletion()
{
}


BEGIN_MESSAGE_MAP(CComboCompletion, CComboBox)
    //{{AFX_MSG_MAP(CComboCompletion)
    ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditUpdate)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// 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) 
      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);
}

License

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
The Code Project
Canada Canada

Member

Follow on Twitter Follow on Twitter
Google+
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCComboCOmpletion in two dialogs PinmemberAnu_Bala19:25 27 Jul '08  
GeneralLittle Bug Pinmemberchris17523:56 22 Aug '06  
GeneralRe: Little Bug Pinmemberellolazo9:27 28 Aug '06  
GeneralRe: Little Bug Pinmemberellolazo5:34 31 Aug '06  
GeneralEnter in combobox and default button Pinmemberellolazo6:57 12 Jun '06  
GeneralHelp..Enter key PinmemberG.Inbakumar5:29 20 Feb '06  
QuestionGreat Works,Can you complete it with C#? Pinmemberkv400014:59 8 Jun '05  
AnswerRe: Great Works,Can you complete it with C#? PinmemberBob-ish11:51 27 Aug '05  
kv4000 wrote:
Can you complete it with C#? thanks a lot!
 
I just want to add my voice to this, since as a VB.NET programmer I can use C# code by converting it to VB but I'm really ignorant of C++ code.
 
It would really be helpful to me if it's not too much trouble to convert it to C#.
 
At the same time, what would be involved in incorporating the case-sensitive aspects as implemented by Paul S. Vickery? That would solve a lot of peoples' problems I'll bet!
Paul's project is here:
http://www.codeguru.com/Cpp/controls/combobox/article.php/c4957/
 
Thanks!!
GeneralSuggestion Pinsussstarschen17:48 20 Apr '05  
GeneralBackspace and Enter key Pinmemberrlaley1:54 28 Oct '04  
GeneralRe: Backspace and Enter key Pinmemberrlaley16:21 31 Oct '04  
GeneralRe: Backspace and Enter key PinsussReDFoX4:23 20 Feb '05  
GeneralRe: Backspace and Enter key Pinmemberrlaley17:20 20 Feb '05  
GeneralCapturing a right-click on the combo box PinmemberFlic17:07 13 Feb '03  
GeneralRe: Capturing a right-click on the combo box Pinmemberabdulsoni20:29 18 Dec '03  
GeneralView options PinmemberAnonymous4:16 9 May '02  
GeneralSimple-type ComboBox Highlight PinmemberMaxwell B.13:49 6 Jan '02  
GeneralGetCurSel() for the CComboBox is returns 2 different values on consecutive calls PinmemberEd Mitchell3:23 3 Oct '01  
GeneralCBN_SELENDOK and CBN_SELCHANGE PinmemberMichael Martin3:25 13 Sep '01  
GeneralRe: CBN_SELENDOK and CBN_SELCHANGE PinmemberCarlos Antollini3:53 13 Sep '01  
GeneralGetCurSel() doesn't return the selected index. PinmemberKenneth Chen20:27 2 Aug '01  
GeneralRe: GetCurSel() doesn't return the selected index. PinmemberKyle Poole7:03 17 May '03  
GeneralRe: GetCurSel() doesn't return the selected index. PinmemberM. Wohlers1:43 3 Jan '05  
GeneralUsing your Class in a non Dialog based app for example in a SDI/MDI embedded in toolbar & Rebar similar to IE 5 PinmemberSean3:03 25 Jan '01  
GeneralRe: Using your Class in a non Dialog based app for example in a SDI/MDI embedded in toolbar & Rebar similar to IE 5 PinmemberChris Sims9:34 18 Jul '01  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 18 Oct 2000
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid