Jump to content
Incus J

TMemo - How to select (highlight) a line of text in code?

Recommended Posts

I'd like to search for, and then highlight a line or phrase of text in a TMemo.  I can get the Caret to move to the start of the correct location via SelStart, but when I set SelLength the selection does not expand visibly.  The text highlight does not appear.  Here's my approach :

s := 'section';
i := MyMemo.Lines.Text.IndexOf('['+s+']');
if i>-1 then begin
  MyMemo.SetFocus;
  MyMemo.SelStart := i+1;
  MyMemo.SelLength := s.Length;
  MyMemo.Repaint;
  //MyMemo.ScrollTo(MyMemo.Caret.Pos.Y,0);

I suspect the selection is actually being made successfully, internally behind the scenes, but the control is failing to highlight its current selection.  If I start typing, the not-visibly-selected text is replaced with what I type.  I added Repaint to see whether that would give the control a gentle kick to persuade it to display its selection highlight, but no joy so far.  I can highlight text with the mouse OK, and the control is set to display its highlight whether focussed or not (HideSelectionOnExit is false).  The memo is not currently ReadOnly.  I've also tried toggling the control type between Platform and Styled, but the behaviour remains the same.

 

(You can also spot that I'd eventually like to scroll the selected line into view, and I'd also like this to happen on a ReadOnly memo without giving the memo focus - but one step at a time.)

Share this post


Link to post

Thanks Uwe,

 

I've had a look in the Object Inspector, and in the documentation, but there doesn't seem to be a HideSelection property on TMemo.  However there is HideSelectionOnExit.  I have that set that to false at the moment, but no joy - when I make a selection in code via SelStart and SelLength, no highlight is displayed.

Share this post


Link to post

Looks like a bug, but you can do:

var x: TCaretPosition;
begin
  x.Line:=5;
  x.Pos:=1;
  MyMemo.Model.SelectText(x, 50);


(Using Delphi XE5 in that version Selstart and Sellength worked as expected.)
 

Edited by Lajos Juhász
  • Thanks 1

Share this post


Link to post
12 hours ago, Incus J said:

I've had a look in the Object Inspector, and in the documentation, but there doesn't seem to be a HideSelection property on TMemo.

Sorry, I just realized that we are on FMX here.

Share this post


Link to post
On 10/9/2020 at 9:28 PM, Lajos Juhász said:

var x: TCaretPosition;
begin
  x.Line:=5;
  x.Pos:=1;
  MyMemo.Model.SelectText(x, 50);

I've just tried your suggestion, and it works perfectly.  I don't think I would ever have thought to look under .Model for a SelectText method.  It even scrolls the line into view, and it works when the memo is ReadOnly too.  Thank you Lajos!

  • Thanks 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×