Click here to Skip to main content
15,891,905 members
Articles / Desktop Programming / MFC
Article

AutoComplete without IAutoComplete

Rate me:
Please Sign up or sign in to vote.
4.90/5 (46 votes)
30 Apr 2003CPOL3 min read 370.1K   8K   120   58
An AutoCompletion control that doesn't use IAutoComplete but still retains the same look and feel.

Sample Image

Introduction

There have been already several articles on AutoComplete functions on CodeProject. Many subclass a CEdit or CComboBox and complete the input in the control or show a variety in the dropdown list. Another option is to use the IAutoComplete with IEnumString and quite a lot of other I functions.

When I saw the WTL-AutoComplete function by Klaus Probst, I thought, "cool, but all that should be working even without Internet Explorer and cryptic calls". And, even more, fairly easily.

How to use

Copy the files (ACEdit.cpp/h + ACListWnd.cpp/h) into the project directory, then add them to the project and insert the header, preferably in stdafx.h.

#include "ACEdit.h"
Declare a CEdit or a CComboBox and subclass it to CACEdit
bevorehand:
CEdit m_EditCtrl1;

afterwards:
CACEdit m_EditCtrl1;

Afterwards initialise the control and define the mode, e.g. in OnInitDialog.

m_EditCtrl1.Init();
m_EditCtrl1.SetMode(); // default = _MODE_STANDARD_       

Possible modes could be:

  • _MODE_STANDARD_
  • _MODE_SEPARATION_
  • _MODE_FILESYSTEM_
  • _MODE_FS_ONLY_FILE_
  • _MODE_FS_ONLY_DIR_
  • _MODE_FS_START_DIR_
  • _MODE_SD_ONLY_FILE_
  • _MODE_SD_ONLY_DIR_
  • _MODE_CURSOR_O_LIST_
  • _MODE_FIND_ALL_.

See further down for more explanations. If you forget Init(), the initialisation will be made up later in SetMode().

Finally, insert the strings. There are two different methods to do this: AddSearchString and AddSearchStrings.

m_EditCtrl1.AddSearchString("Test1");
m_EditCtrl1.AddSearchString("Test2");
m_EditCtrl1.AddSearchString("Tiger");
m_EditCtrl1.AddSearchString("Dog");
  
or

static LPCTSTR STRINGS[] = 
{
    _T("Test1"),
    _T("Test2"),
    _T("Tiger"),
    _T("Dog"),
    NULL
};

m_EditCtrl1.AddSearchStrings(STRINGS);    

Before the insertion of the strings, the function AddSearchStrings() calls RemoveSearchAll(), and clears the internal item list of the type CStringArray. AddSearchStrings() can be combined with AddSearchString(), but this doesn’t work vice versa. A specific deletion of strings is not implemented at present.

This is all we need for a simple AutoComplete. Moreover, there is a possibility to implement separators. A m_EditCtrl1.SetSeparator(_T("\\")); causes a \ to function as a beginning or end of a line. If you enter XXX\t\YYY, in our example you will see a list including Test1, Test2 and Tiger.

But if the user enters xxTi, however, this fails. If you use constants like <Parameter1> (constants enclosed in brace characters, in our example <>). Then the possibility is given to extend the command SetSeparator() with a prefix, that is the first sign of your constants. The prefix must not be part of the SearchStrings. However, it does appear in the list and is part of a result.

...
m_EditCtrl1.AddSearchString("PARAMETER1>"); // without prefix!
m_EditCtrl1.AddSearchString("PARAMETER2>");
m_EditCtrl1.AddSearchString("PARAMETER3>");
m_EditCtrl1.SetSeparator("<",'<');
...     

This example yields the result as well if entered XXX< or /<.

And AutoComplete for the file system:

...
m_DirEdit.SetMode(_MODE_FILESYSTEM_);
...

Now, when you start typing a path, the control will drop down a list with paths that match what you've typed so far - run-command in the start menu (in Win2K or on machines with IE 5.0).

SetMode(_MODE_FS_ONLY_FILE_) lists files only and SetMode(_MODE_FS_ONLY_DIR_) only lists directories.

m_DirEdit.SetMode(_MODE_FS_START_DIR_);
m_DirEdit.SetStartDirectory(_T("C:\\Windows\\"));
...      
If you use either SetMode() _MODE_FS_START_DIR_, _MODE_SD_ONLY_FILE_ or _MODE_SD_ONLY_DIR_,  a directory can be indicated by SetStartDirectory().

In the example above, the control lists all files in C:\Windows, but in contrast to _MODE_FILESYSTEM_ without showing the path (C:\Windows).

Version 1.2

-Fix: _MODE_SD_ONLY_DIR_
-Fix: OnActivateApp() VC6/VC7 compiler adaptation
-Fix: OnGetMinMaxInfo() following the suggestions of "yogurt" (cp. comments)
-_MODE_FIND_ALL_ for SetMode() if you enter on, the function finds One, One1, Melon, Lemon, ...

Version 1.1

ComboBox-Support:
-int AddString( LPCTSTR lpszString);
-int GetLBText( int nIndex, LPTSTR lpszText );
-void GetLBText( int nIndex, CString& rString );
-int SetDroppedWidth(UINT nWidth);
-int FindString( int nStartAfter, LPCTSTR lpszString );
-int SelectString( int nStartAfter, LPCTSTR lpszString );
-void ShowDropDown(BOOL bShowIt = TRUE );
-void ResetContent();
-int GetCurSel();

and

_MODE_CURSOR_O_LIST_ (Open the List with Corsorkeys)

If this flag is set with SetMode(), it is already possible to indicate the list of the search strings with the cursor keys (UP/DOWN) in an empty input field. This works only with a CEdit control, however, as the cursor keys in a ComboBox have different functions.

Conclusion

The control looks like the function in Windows (which triggered the project), but it works entirely without IAutoComplete. There are definitely various ways to extend it, but as it works the way it should, I’m fine with it. Hopefully, the control will be of use to you – I had fun writing it.

Sources:

License

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


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow display dropdown list wider then edit? Pin
liubl16-Apr-17 22:12
liubl16-Apr-17 22:12 
Questioncrash while exit Pin
peilinok17-Aug-14 21:56
peilinok17-Aug-14 21:56 
AnswerRe: crash while exit Pin
peilinok17-Aug-14 23:43
peilinok17-Aug-14 23:43 
GeneralMy vote of 5 Pin
Member 1020317918-May-14 22:18
Member 1020317918-May-14 22:18 
NewsMy code based on the ACListWnd Pin
Yaukey28-Apr-13 1:45
Yaukey28-Apr-13 1:45 
QuestionCrash while Init Pin
Member 41326255-Dec-12 18:17
Member 41326255-Dec-12 18:17 
QuestionDo you know how to change font size?? Pin
test12344568-May-12 23:56
test12344568-May-12 23:56 
GeneralMy vote of 5 Pin
Yaukey14-Jun-11 5:31
Yaukey14-Jun-11 5:31 
GeneralImprovements + coding style Pin
Yogurt5-May-11 1:12
Yogurt5-May-11 1:12 
In CACListWnd::DrawItem rcLabel.right shouldn't be width+m_SM_CXBORDER instead of the minus?

In the same function you may honour right-to-left languages. Just change to DrawText call to this:

UINT    format = DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS;
if ((::GetWindowLong (m_hWnd, GWL_EXSTYLE) & (WS_EX_RIGHT | WS_EX_RTLREADING)) != 0)
    format |= DT_RIGHT;
else
    format |= DT_LEFT;
pDC->DrawText (m_DisplayStr, -1, rcLabel, format);


In CACListWnd::Init you should increase the item height a bit. Several languages use characters that have accent over capital letters such as ÁÅŠ.

In CACListWnd::OnNcCalcSize is there any sense for inflating a rect with 0 units, or they should be -1?

If _DOPAINTMESSAGELOOP_ is undefined, it won't compile due to references to DoPaintMessageLoop.

You can drastically speed up CACListWnd::FindString for large number of search strings if you put the for loop within the if(m_lMode & _MODE_FIND_ALL_), etc. And in the _MODE_FIND_EXACT_ branch you can use _tcsncicmp to avoid m_Str1.MakeUpper() calls.

If you declare CACListWnd::CompareString as __cdecl, you can compile the project with different settings.

In CACListWnd::CompareString you should use _tcsicmp and TCHAR* (or rather const TCHAR* const*) instead of the ANSI-only versions.

By the way, using the CompareString name is a bad idea since it's an existing WinAPI function. The qsort call may not compile too well with certain includes.

CACListWnd::SortList can be made static.

Some people use more strict compiler warnings (or company policy, or code review, or PC-Lint) than you. There are a few improvement thoughts:

It is a habit (the so-called Hungarian naming convention) to use the m_ prefix for member variables. You use it for method parameters and local variables. Please, don't.

CACEdit::SetEditSel shouldn't be int instead of BOOL, as you return various values that are not TRUE or FALSE?

You use the ! operator to test numbers for zero but the == false expression to test a bool value. Why?

The ShowWindow parameters better be capitalized TRUE or FALSE.

A lot of Get* methods could be const. The ScrollBarWidth method could be renamed to GetScrollBarWidth.

The { } brackets for if and for blocks should be aligned to each other.
GeneralRe: Improvements + coding style Pin
Andreas Kapust5-May-11 4:08
Andreas Kapust5-May-11 4:08 
GeneralRe: I fix some small bugs .... Pin
buingocdinh12-Dec-10 5:26
buingocdinh12-Dec-10 5:26 
GeneralThanks Pin
tayyebe21-Sep-10 0:00
tayyebe21-Sep-10 0:00 
AnswerA great one Pin
auge__8-Jun-10 22:01
auge__8-Jun-10 22:01 
GeneralThanks Andreas Pin
andywebsdale8-Jun-10 4:02
andywebsdale8-Jun-10 4:02 
GeneralATL/MFC version Pin
Member 40907114-Feb-09 6:37
Member 40907114-Feb-09 6:37 
Questionhow to use in vs2005? Pin
AchillesTangYan26-Nov-08 18:41
AchillesTangYan26-Nov-08 18:41 
GeneralWorks for me... Pin
chuck7878-Nov-08 6:32
chuck7878-Nov-08 6:32 
GeneralNot UNICODE compliant Pin
Borislav Stanimirov26-Oct-08 1:15
professionalBorislav Stanimirov26-Oct-08 1:15 
GeneralRe: Not UNICODE compliant Pin
auge__8-Jun-10 22:20
auge__8-Jun-10 22:20 
GeneralRe: Not UNICODE compliant Pin
Andreas Kapust9-Jun-10 2:50
Andreas Kapust9-Jun-10 2:50 
QuestionWhy ACListWnd cannot receive WM_MOUSEWHEEL message? Pin
miracle@163.com25-Jun-08 16:25
miracle@163.com25-Jun-08 16:25 
QuestionDoes not set cur selection? Pin
AbsoluteSupport12-Dec-06 18:04
AbsoluteSupport12-Dec-06 18:04 
AnswerRe: Does not set cur selection? Pin
mesajflaviu30-Sep-10 5:38
mesajflaviu30-Sep-10 5:38 
GeneralExcellent Effort Pin
kklowanshi3-Nov-06 4:51
kklowanshi3-Nov-06 4:51 
GeneralOnEnChangeEditXXX and Missing CEdit Methods Pin
intripoon9-Oct-05 5:34
intripoon9-Oct-05 5:34 

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.