Get the caret position (line and char) in an FMX TMemo

1.2k views Asked by At

I try to show the position of a memo's caret in a statusbar which contains two labels.

I tried this:

lblX.Text := Memo.Caret.Pos.X.ToString();

lblY.Text := Memo.Caret.Pos.Y.ToString();

The two values seems to represent the real position from left and top of the memo.

Is it possible to get it as row (lines) and cols (chars)?

I want to clarify that I work with firemonkey in order to be able to compile my project towards windows and linux.

Thank you already for your answers.

Selticq.

2

There are 2 answers

1
Andreas Rejbrand On

I have never used FMX before, but using Code Insight I immediately found that Memo.CaretPosition.Line and Memo.CaretPosition.Pos represent the current line and column, respectively.

This is confirmed by the documentation:

Line represents the number of the line containing the cursor, indexed from zero.

Pos represents the horizontal character coordinate of the cursor, indexed from zero.

[...]

Thus, if Line = 3 and Pos = 5, then the cursor is at the fourth line and at the sixth character from the start of the line.

1
fpiette On

If you want to display the memo caret position, you can use code like this:

procedure TForm1.UpdateCaretPosDisplay;
begin
    lblX.Text := (Memo1.CaretPosition.Pos + 1).ToString;
    lblY.Text := (Memo1.CaretPosition.Line + 1).ToString;
end;

And if you want a complete sample code with that method called at the correct event handlers, here it is:

unit FmxMemoCaretPosDemoMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types,
  FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    StatusBar1: TStatusBar;
    lblX: TLabel;
    lblY: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
    procedure Memo1Enter(Sender: TObject);
    procedure Memo1KeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift:
        TShiftState);
    procedure Memo1MouseUp(Sender: TObject; Button: TMouseButton; Shift:
        TShiftState; X, Y: Single);
  private
    procedure UpdateCaretPosDisplay;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
begin
    UpdateCaretPosDisplay;
    ActiveControl := Memo1;
end;

procedure TForm1.Memo1Change(Sender: TObject);
begin
    UpdateCaretPosDisplay;
end;

procedure TForm1.Memo1Enter(Sender: TObject);
begin
    UpdateCaretPosDisplay;
end;

procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
    Shift: TShiftState);
begin
    UpdateCaretPosDisplay;
end;

procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton; Shift:
    TShiftState; X, Y: Single);
begin
    UpdateCaretPosDisplay;
end;

procedure TForm1.UpdateCaretPosDisplay;
begin
    lblX.Text := (Memo1.CaretPosition.Pos + 1).ToString;
    lblY.Text := (Memo1.CaretPosition.Line + 1).ToString;
end;

end.