Mirror

How to get the current row and column of a cursor in a TRichEdit (Views: 10)


Problem/Question/Abstract:

How to get the current row and column of a cursor in a TRichEdit

Answer:

procedure TForm1.GetPosition(Sender: TRichEdit);
var
  aX, aY: Integer;
  TheRichEdit: TRichEdit;
begin
  aX := 0;
  aY := 0;
  TheRichEdit := TRichEdit(Sender);
  aY := SendMessage(TheRichEdit.Handle, EM_LINEFROMCHAR, TheRichEdit.SelStart, 0);
  aX := TheRichEdit.SelStart - SendMessage(TheRichEdit.Handle, EM_LINEINDEX, aY, 0);
  Panel1.Caption := IntToStr(aY + 1) + ':' + IntToStr(aX + 1);
end;

procedure TForm1.RichEditMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  GetPosition(RichEdit);
end;

procedure TForm1.RichEditKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  GetPosition(RichEdit);
end;

<< Back to main page