WPCubed Product Support

adding autocomplete feature

I want to incorporate an autocomplete feature to an app using a TDBWPRichText edit window so that users do not need to continually type out names of cities, clients, and other commonly repeated terms. Has anyone attempted this? Are there any common problems such as response lag issues? Can the code of WPSpell possibly be modified to add this feature? Any ideas appreciated.

#1 ♥ 0
8.7k Views 5 users read 3 min

Hi ttheimer,

I have implemented such feature, just like MSWord does with Autotexts. It has hint window and so on, and works fine.

You just need to handle two events:

  • OnKeyPress:
    • To check for user typing any char <> #13 and <> #0, so it check for available autocomplete
    • To check for user typing #13, so it executes the autocomplete
  • OnKeyDown: To hide the hint window

To decide if there are available autocomplete, you just need to check for previous words. Here you may have problems depending of your algorithm, for instance, to decide how many characters a word should have to check for autocomplete. In my case, it starts checking only for words with more than 4 characters.

After that, you just need to execute the replace, that in my case I used SelText / SelectionAsString, because I leave the user decide if uses or not text format. The text selection may be tricky, and in my case I used selStart and selLength, instead of SetSelPosLen, that wasn't working fine.

About lag issues, I have none. Works fine 🙂

Hope this helps

Alessandro Fragnani

#2 ♥ 0
support

Q: I want to incorporate an autocomplete feature ...

That is actually quite easy to do. Use the event "AfterCompleteWordEvent".

procedure TWPTBXForm.WPRichText1AfterCompleteWordEvent(Sender: TObject;
  var lastchar: Char);
begin
  if lastchar = #32 then
  begin
    if WPRichText1.CPWord = 'mfg' then
    begin
      WPRichText1.CPWord := 'Mit freundlichen Grüssen';
    end;
  end
  else if lastchar = '+' then
    if WPRichText1.CPWord = 'mfg' then
    begin
      WPRichText1.CPWord := 'Mit freundlichen Grüssen';
    end;
end;

Of course it is also possible to use InputString to insert some text, or insert a graphic.

Julian

#4 ♥ 0

Fragani, How we can show then hint window?

For sample, if user type AC show at hint window "AC - Autocomplete"

Hou we can do this?

#5 ♥ 0

Hi Marcio

I had to work with some steps:

  1. At OnKeyPress, check for possible autocompletes. I only recognize the 'prefix' starting at the forth character, like MSWord does
  2. If has, show the hint. I created my own THintWindow
  3. Also at OnKeyPress, if the key is ENTER, I replace the 'prefix' by the autocomplete text
  4. In some places, like MouseDown, KeyDown and KeyPress (when is a different key), I release the HintWindow.

Finding the 'prefix' is the worst algorithm, because I had to work with WPWordDelimiterArray and par.ANSIChr, to ignore invalid characters. The rest, is simple.

Alessandro

#6 ♥ 0

Addict Spell Check handles this for me.

#7 ♥ 0

I have Addict (version 3.4.3), but I can't find it. It only has AutoReplace, which does not:

  1. Shows a hint window with the 'replaceable text'
  2. Allow 'replaceable text' to be RTF/HTML Format

Doing the autocomplete myself, I was able to add this features

Alessandro

#8 ♥ 0
support

Auto completion is easy added to WPTools

simply use event "AfterCompleteWordEvent":

procedure TWPTBXForm.WPRichText1AfterCompleteWordEvent(Sender: TObject;
  var lastchar: Char);
begin
  if lastchar = #32 then
  begin
    if WPRichText1.CPWord = 'mfg' then
    begin
      WPRichText1.CPWord := 'Mit freundlichen Grüssen';
    end;
  end
  else if lastchar = '+' then
    if WPRichText1.CPWord = 'mfg' then
    begin
      WPRichText1.CPWord := 'Mit freundlichen Grüssen';
    end;
end;

Regards,
Julian

#9 ♥ 0
This category is archived. No new posts are possible.