Click here to Skip to main content
12,352,294 members (53,285 online)
Click here to Skip to main content
Add your own
alternative version

Stats

172.9K views
6K downloads
84 bookmarked
Posted

Autocomplection in RichEdit Control

, 11 Jul 2002 Public Domain
Rate this:
Please Sign up or sign in to vote.
A control that allows to use an autocomplection feature in RichEdit controls as the one in Visual Studio
<!-- Download Links --> <!-- Article image -->

Sample Image - AutocomplectionCtrl.gif

<!-- Add the rest of your HTML here -->

CAutocomplectionCtrl is a control designed to add the auto completion feature in a CRichEditCtrl. You can easily modify the code to add the auto completion feature in a CRichEditView.

You can specify options such as displaying all known words or only the matching ones.

User can start the auto completion feature pressing Ctrl+space; he can confirm the selected word pressing space, Enter or a non-alpha char; he can use up, down, pgUp, pgDown, home, end, Ctrl+Space or mouse-wheel to switch between the words in the list; he can use left, right to modify the matching part of the word.

Using the control

Using ClassWizard add a variable in your dialog class; the variable must be a CAutocomplectionCtrl control; if the ClassWizard displays only CRichEdit select it, and change it to CAutocomplectionCtrl in the .h file.

Remember to include "Autocomplection.h" in your .h file and to insert the line

AfxInitRichEdit();

in the Initinstance() function of your application.

Then you must define your dictionary using the AddKeyword() funcion; usually you'll do that in the OnInitDialog function. In the sample application I read the words from a file, so resulting code is:

while (file.ReadString(keyword))
	m_edit.AddKeyword(keyword);

Well, your work is done Smile | :)

If you like you can specify some options...

Option functions

void Enable(BOOL);

You can enable or disable the auto completion feature; by default it's enabled.

void ShowListBox(BOOL);

You can select if to display the listbox or not

void ListAllWords(BOOL);

You can select if to display in the listbox all known words, or only the matching ones

void TrapEnter(BOOL);

Pressing Enter, the selected word is confirmed.

If TrapEnter is set to FALSE, when pressing Enter, the control will add a new line char, if TrapEnter is set to TRUE, the new line char will not be displayed

void CaseSensitive(BOOL);

You can select if the matching function must be case sensitive or not.

void AutoLearn(BOOL);

That feature isn't very useful; if enabled, when the user types an unknown word, the control will add it to the dictionary; that could be nice if the user never mistakes typing words...

void LearnOnDblClick(BOOL);

If this feature is enabled, when the user double-clicks over a word, the control learns it.

Dictionary functions

void GetDictionary(CStringArray& dictionary);

This function fills the given CStringArray with all known words.


void ResetDictionary();

This function empties the dictionary.


BOOL AddKeyword(const CString& str);

This function adds the specified word in the dictionary.


BOOL IsKeyword(const CString& str);


This function checks if the specified word is in the dictionary.

Developing notes

The code compiles cleanly under the warning level 4.

Search

I chose to insert word in alphabetic order and to find them using a binary search, so the search function is O(log(n)).

Case sense

Word matching can be case sensitive or not case sensitive. Instead of checking anytime or duplicate the code I decided to use function pointers...

Word recognition

Word recognition is done looking for the left-nearest "space" character. You can easily change your word recognition modifying the line

while (from && buffer[from]!=' ')
with a such line
while (from && buffer[from]!=' ' && buffer[from]!='(' && buffer[from]!='=')

Further improvements (aka TODO)

The learn feature could be improved; anyhow the applicative context suggests different improvement modalities: e.g., you could want to learn only frequent typed words, or only the # nearest words, or....

You could like to display an icon near the word (yes, like Visual Studio)... so you should replace the CArrayString with an array class and a class containing the string and an ID specifying the icon.

Sometimes (when there are many matching words) filling CListBox is slow. It could be useful to replace the CListBox with a faster one (maybe a owner data listbox...)

History

12 July 2002 - First public release

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Paolo Vernazza
Software Developer (Senior) Ermit
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionWhat Licenses in this under? Pin
matt1.anderson@ge.com15-Oct-14 12:16
membermatt1.anderson@ge.com15-Oct-14 12:16 
AnswerRe: What Licenses in this under? Pin
Paolo Vernazza24-Dec-15 0:59
memberPaolo Vernazza24-Dec-15 0:59 
QuestionAutocomplection in RichEdit Control - License Pin
Member 1086227328-Jul-14 11:41
memberMember 1086227328-Jul-14 11:41 
GeneralThis is not working for Japanese text Pin
Member 9328194-Jun-10 1:56
memberMember 9328194-Jun-10 1:56 
GeneralIncorrect twips conversion Pin
Dan East14-Dec-07 18:46
memberDan East14-Dec-07 18:46 
GeneralVC8 compilation issues : error C2065: 'i' : undeclared identifier [modified] Pin
carlosrt27-Sep-06 2:27
membercarlosrt27-Sep-06 2:27 
Generalpb with download Pin
hiko-seijuro26-Jun-05 21:41
memberhiko-seijuro26-Jun-05 21:41 
Questionhow to add image Pin
nevis9-Jul-04 3:29
sussnevis9-Jul-04 3:29 
GeneralSelect list item with mouse Pin
Adrian Gibbons3-Feb-04 4:03
memberAdrian Gibbons3-Feb-04 4:03 
GeneralRe: Select list item with mouse Pin
sammyc3-Jun-04 19:33
membersammyc3-Jun-04 19:33 
GeneralRe: Select list item with mouse Pin
Adrian Gibbons3-Jun-04 23:32
memberAdrian Gibbons3-Jun-04 23:32 
GeneralRe: Select list item with mouse Pin
sammyc3-Jun-04 22:06
membersammyc3-Jun-04 22:06 
GeneralProblem with scrolling Pin
arikgreen6-Sep-03 14:15
memberarikgreen6-Sep-03 14:15 
Generaldo not appear inside a text Pin
mmueller22-May-03 8:14
membermmueller22-May-03 8:14 
Generalbug with tabs Pin
mmueller22-May-03 7:41
membermmueller22-May-03 7:41 
GeneralRe: bug with tabs Pin
Ganesan Subramaniam15-Jan-04 3:44
sussGanesan Subramaniam15-Jan-04 3:44 
GeneralListbox disapears Pin
Skipla20-Feb-03 12:14
memberSkipla20-Feb-03 12:14 
GeneralRe: Listbox disapears Pin
sammyc3-Jun-04 19:36
membersammyc3-Jun-04 19:36 
Generalbug: list box leaves artifacts Pin
hector80111-Feb-03 20:18
memberhector80111-Feb-03 20:18 
QuestionHow to implement in CRichEditView? Pin
RickGavin5-Aug-02 20:42
memberRickGavin5-Aug-02 20:42 
AnswerRe: How to implement in CRichEditView? Pin
Paolo Vernazza7-Aug-02 1:30
memberPaolo Vernazza7-Aug-02 1:30 
GeneralRe: How to implement in CRichEditView? Pin
allelimo18-Nov-02 3:23
memberallelimo18-Nov-02 3:23 
GeneralRe: How to implement in CRichEditView? Pin
Adrian Gibbons11-Dec-03 2:05
memberAdrian Gibbons11-Dec-03 2:05 
AnswerRe: How to implement in CRichEditView? Pin
worldtree113-Apr-06 18:26
memberworldtree113-Apr-06 18:26 
GeneralThis is Great Pin
Colin Davies14-Jul-02 19:32
memberColin Davies14-Jul-02 19:32 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 12 Jul 2002
Article Copyright 2002 by Paolo Vernazza
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid