Search code examples
delphiautocompletememo

Delphi - AutoComplete Memo


I am requiring a Memo with Auto-completion functionality. Ultimately, I would like the ability to display a custom auto-completion list when the user presses a hotkey (Ctrl-space) similar to Delphi IDE auto-completion.

I have the TMS AdvMemo, but to be honest the help for this particular component is lacking. It appears the AdvMemo supports custom auto completion, but I cant seem to find out how to display a list.

So, if anyone has any suggestions to achieve auto completion on a memo, or to enlighten me as the use of the AdvMemo, it would be appreciated


Solution

  • I decided to write some handlers for a TMemo using a TPopupmenu as the autocomplete list.

    For those that read this please refer to my other post: Delphi - Get the whole word where the caret is in a memo (thanks to RRUZ)

    And the following code: OnPopup for the AutoComplete TPopupMenu: (memoAutoComplete hold the list of autocomplete items)

    procedure AutoCompletePopup(Sender: TObject);
    var i : integer;
    NewItem : TMenuItem;
    AutoCompleteToken: String;
    begin
        //filter list by token
        AutoCompleteToken := SelectWordUnderCaret(edtComment);
        AutoComplete.Items.Clear;
        for i:=0 to memoAutoComplete.Lines.Count -1 do
        begin
             if SameText(LeftStr(memoAutoComplete.Lines.Strings[i],Length(AutoCompleteToken)),AutoCompleteToken) then
             begin
                 NewItem := TMenuItem.Create(AutoComplete);
                 NewItem.Caption := memoAutoComplete.Lines.Strings[i];
                 NewItem.OnClick := AutoComplete1Click;
                 NewItem.OnMeasureItem := AutoComplete1MeasureItem;
                 NewItem.OnAdvancedDrawItem := AutoComplete1AdvancedDrawItem;
                 AutoComplete.Items.Add(NewItem);
             end;
        end;
    end;
    

    And for the Tmemo:

    procedure Memo1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var pt : TPoint;
    begin
         if (Key = VK_SPACE) and (GetKeyState(VK_CONTROL) < 0) then
         begin
              pt := Memo1.ClientToScreen(Point(0,Memo1.Height));
              AutoComplete.Popup(pt.X,pt.Y);
         end;
    end;