Click here to Skip to main content
6,304,948 members and growing! (18,979 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » General     Intermediate

Autocomplection in RichEdit Control

By Paolo Vernazza

A control that allows to use an autocomplection feature in RichEdit controls as the one in Visual Studio
VC6, VC7Win2K, WinXP, MFC, Dev
Posted:10 Jul 2002
Updated:11 Jul 2002
Views:131,352
Bookmarked:57 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
24 votes for this article.
Popularity: 6.48 Rating: 4.69 out of 5
1 vote, 5.3%
1

2
2 votes, 10.5%
3
1 vote, 5.3%
4
15 votes, 78.9%
5

Sample Image - AutocomplectionCtrl.gif

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 :)

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Paolo Vernazza


Member

Occupation: Web Developer
Location: Italy Italy

Other popular Edit Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralIncorrect twips conversion PinmemberDan East19:46 14 Dec '07  
GeneralVC8 compilation issues : error C2065: 'i' : undeclared identifier [modified] Pinmembercarlosrt3:27 27 Sep '06  
Generalpb with download Pinmemberhiko-seijuro22:41 26 Jun '05  
Generalhow to add image Pinsussnevis4:29 9 Jul '04  
GeneralSelect list item with mouse PinmemberAdrian Gibbons5:03 3 Feb '04  
GeneralRe: Select list item with mouse Pinmembersammyc20:33 3 Jun '04  
GeneralRe: Select list item with mouse PinmemberAdrian Gibbons0:32 4 Jun '04  
GeneralRe: Select list item with mouse Pinmembersammyc23:06 3 Jun '04  
GeneralProblem with scrolling Pinmemberarikgreen15:15 6 Sep '03  
Generaldo not appear inside a text Pinmembermmueller9:14 22 May '03  
Generalbug with tabs Pinmembermmueller8:41 22 May '03  
GeneralRe: bug with tabs PinsussGanesan Subramaniam4:44 15 Jan '04  
GeneralListbox disapears PinmemberSkipla13:14 20 Feb '03  
GeneralRe: Listbox disapears Pinmembersammyc20:36 3 Jun '04  
Generalbug: list box leaves artifacts Pinmemberhector801121:18 1 Feb '03  
GeneralHow to implement in CRichEditView? PinmemberRickGavin21:42 5 Aug '02  
GeneralRe: How to implement in CRichEditView? PinmemberPaolo Vernazza2:30 7 Aug '02  
GeneralRe: How to implement in CRichEditView? Pinmemberallelimo4:23 18 Nov '02  
GeneralRe: How to implement in CRichEditView? PinmemberAdrian Gibbons3:05 11 Dec '03  
AnswerRe: How to implement in CRichEditView? Pinmemberworldtree119:26 13 Apr '06  
GeneralThis is Great PinmemberColin Davies20:32 14 Jul '02  
Generalhow to show the list?? Pinmemberbenben4:52 14 Jul '02  
GeneralRe: how to show the list?? PinmemberPaolo Vernazza15:36 14 Jul '02  
Generalbusted image Pinmemberbryce17:00 11 Jul '02  
GeneralRe: busted image PinmemberPaolo Vernazza22:52 11 Jul '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jul 2002
Editor: Chris Maunder
Copyright 2002 by Paolo Vernazza
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project