Tips and Frequently Asked Questions
FullWordAtXY() and TokenAtXY() methods
FullWordAtXY() returns the full word found at the cursor position. This is the word within word boundaries.
The word boundary is a space character, so FullWordAtXY() returns the series of characters till a space is encountered.
TokenAtXY() returns the series of characters till a token delimiter is encountered.
A token is delimited by the characters: [#32, ''('', '')'', ''['', '']'', '','', ''.'', '':'', '';'', ''"'', '''''''', ''='']
How to set the cursor position at the beginning of a line when clicking in the TAdvMemo
Set AdvMemo.AutoExpand = false
How to synchronise the vertical scrolling of 2 TAdvMemo Objects
From the OnTopLeftChanged event, set the AdvMemo.TopLine from the target memo equal to the TopLine value of the sender memo.
How to control the vertical scroll position
In TadvMemo, you programmatically control the vertical scroll position with the property:
How to use bookmarks
You can add a bookmark on a specific line with:
advmemo.Bookmarks[bookmarkindex] := linenumber;
You can retrieve the linenumber for a specific bookmark index with:
advmemo.Bookmarks[bookmarkindex]: integer;
Using :
advmemo.BookmarkIndex[bookmarkindex]: integer
is just for putting an extra number on the book mark icon in the gutter.
Example:
procedure TForm1.FormCreate(Sender: TObject); begin
// load some file
advmemo1.Lines.LoadFromFile('somefile.txt');
// set first bookmark at line 100
advmemo1.Bookmarks[0] := 100;
// set second bookmark at line 200
advmemo1.Bookmarks[1] := 200;
end;
// code to move to the first bookmark, i.e. jump to line 100; begin
advmemo1.GotoBookmark(0);
end;
How to insert text in a new line
You can use the function:
AdvMemo.MouseToCursor(X,Y, var CurX, CurY: integer);
to convert a mouse X,Y coordinate a cursor coordinate in the memo and use CurY as line index.
How to create own Syntax Highlighters with customer specified keywords
As TAdvMemo is a syntax highlighting memo control, this means that the colors of text are defined by syntax rules. If you want to change the color of a specific text in the memo, the only possibility to do this is to define this as a syntax rule.
You can write a custom syntax styler. This is a component that descends from TAdvCustomMemoStyler and defines all syntax rules to control the syntax highlighting in the memo. All included styler components are based on this.
As an example, you can
download the TAdvPascalMemoStyler (ADVMPS.PAS), which is the styler used to set the colors for the Pascal language. If you want to set your own rules for applying color to text in the memo, you'd need to create a similar styler component as TAdvPascalMemoStyler and assign your own styler control to AdvMemo.SyntaxStyles.
How to show a breakpoint indicator for a line
You can set a breakpoint on a line with:
advmemo.BreakPoint[lineindex] := true;
You can set it on multiple lines.
How to use Autocompletion in TAdvMemo
If you drop a TAdvMemo & TAdvPascalMemoStyler on the form and link the styler to the memo, you can type "Show" and then Ctrl-Space and you will see the autocompletion.
Additional words to auto complete can be added via : styler.Autocompletion
How to check if a TAdvMemo is empty
You can use:
if (advmemo.Lines.RealCount = 0) then ...
How to add Emoticons programatically
Easiest way to extend / modify this is to start from AdvMes.pas.
You can see the override of the method DrawKeyword in the TAdvEmoticonMemoStyler class.
To add/change emoticons, you can add/change emoticons in the resource file advmes.res and change/extend the DrawKeyword function to draw emoticons for different character combinations.
Using the design time editor at runtime
Using the design time editor at runtime is easy. First of all, add the unit uMemoEdit to the uses list. If is desired a memo is being edited at runtime using the design-time editor, following code can be used:
procedure TForm2.Button1Click(Sender: TObject);
var
me: TTMSMemoEdit;
begin
me := TTMSMemoEdit.Create(self);
try
me.AdvMemo1.Lines.Assign(AdvMemo1.Lines);
if me.ShowModal = mrOK then
AdvMemo1.Lines.Assign(me.AdvMemo1.Lines);
finally
me.Free;
end;
end;
Programmatically find and select text in the memo
Following code snippet shows how easy it is to programmatically search for the first occurence of text in the memo and select it:
begin
advmemo1.FindText('text to find',[frDown]);
advmemo1.SetFocus;
end;
The search is from the memo cursor position. To search next occurences, just call this code again.
The font keeps resetting to FixedSys when trying to change it
TAdvMemo requires, just like most syntax highlighting editors like the Delphi code editor or Visual Studio code editor, that a fixed size font is used. Examples of such fixed size fonts are Courier, FixedSys, Lucida, Terminal... When trying to set a non fixed font (like Tahoma, Arial), TAdvMemo will reset the font automatically to a fixed size font.