Recent

Author Topic: Memo caret position problem  (Read 2083 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Memo caret position problem
« on: September 15, 2021, 08:09:12 pm »
I pass in a Line and want to display it at an exact position in a memo. The first line string 0, in the memo is:
 
'abcdefghijklmnopqrstuvwxyz'

The call is MemoPosition(Line, 'd');

So I find the 'd' in the first line move four lines down and do memo.lines.add(Line) at the  Memo1.CaretPos .

But this doesn't work. Maybe memo don't work that way.

Need help Please.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.MemoPosition(ALine : String; AMark : string);
  2. var
  3.   i, iPos: SizeInt;
  4. begin
  5.   for i := 0 to Memo1.Lines.Count - 1 do begin
  6.  
  7.     iPos := Pos(AMark, Memo1.Lines[i]);
  8.     if iPos <> 0 then begin
  9.       Memo1.SelLength := 0;
  10.       Memo1.CaretPos := Point(iPos - 1, 4);    < Here
  11.       Memo1.SelStart;
  12. //      Caption := IntToStr(Memo1.SelStart);
  13.       Memo1.Lines.Add( ALine );
  14.  
  15.     end;
  16.   end;
  17. end;  
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Memo caret position problem
« Reply #1 on: September 16, 2021, 12:21:34 am »
no, it does not work that way..

Lines is a Tstrings which works much like a TstringList..

moving the caret does not position where it gets added..

First you need to ensure there are at least blank lines in the list before adding a line so that it shows correctly in the memo..

using the lines property

 Memo.Lines[ LineNUmber] :='asadaasd';

But that only works if the line in the list exist, otherwise you need to keep adding blank lines until you get to the number of lines you want then add the new line.

Memo.Lines.Count etc

The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Memo caret position problem
« Reply #2 on: September 16, 2021, 12:38:29 am »
I don't get it.

I thought a memo was a TStringList which is a glorified array.

You can do the following.
 TheList := TStringList.Create;
         Try
           TheList .LoadFromFile( AFILENAME );
           Memo1.Clear;
           Memo1.Lines.AddStrings( TheList, True );
           ListBox1.Items.Assign(  TheList );                       
         finally
           TheList.Free;
 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

ASerge

  • Hero Member
  • *****
  • Posts: 2246
Re: Memo caret position problem
« Reply #3 on: September 16, 2021, 12:50:22 am »
I think that it is not easy to do it such manner.
First, TMemo works with Utf8 characters, so all string manipulations must be performed using Utf8XXX functions.
Second, line breaks in TMemo are performed both by the line ending sequence and by the width of the visible window. But SelStart is the absolute value from the start of the text, i.e. for lines broken by the LineEnding, it will increase by the length of the LineEnding, for the "width of the window" - no increase. As a result, it is difficult to calculate the SelStart value for the start of a certain line.

It is easier, if you are not afraid of actually changing TMemo, to upload it to TStringList, do operations there, and then load it back.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Memo caret position problem
« Reply #4 on: September 16, 2021, 01:34:39 am »
@ASerge

I think that is the way to go.

This is on the creation of a memo so just create it in a TStringList and then assign it to the Memo1.

I'll try that thanks 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018