Forum > Beginners

Memo caret position problem

(1/1)

JLWest:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.MemoPosition(ALine : String; AMark : string);var  i, iPos: SizeInt;begin  for i := 0 to Memo1.Lines.Count - 1 do begin     iPos := Pos(AMark, Memo1.Lines[i]);    if iPos <> 0 then begin      Memo1.SelLength := 0;      Memo1.CaretPos := Point(iPos - 1, 4);    < Here      Memo1.SelStart;//      Caption := IntToStr(Memo1.SelStart);      Memo1.Lines.Add( ALine );     end;  end;end;  

jamie:
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

JLWest:
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;
 

ASerge:
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:
@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 

Navigation

[0] Message Index

Go to full version