Incus J 10 Posted October 8, 2020 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
Uwe Raabe 2057 Posted October 8, 2020 Have you tried MyMemo.HideSelection := False; ? 1 Share this post Link to post
Incus J 10 Posted October 9, 2020 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
Lajos Juhász 293 Posted October 9, 2020 (edited) 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 October 9, 2020 by Lajos Juhász 1 Share this post Link to post
Uwe Raabe 2057 Posted October 9, 2020 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
Incus J 10 Posted October 12, 2020 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! 2 Share this post Link to post
George Birbilis 1 Posted April 4, 2022 thanks, was also looking for "SelectedText" to get the current selection and it seems it's only available under the Model Share this post Link to post