Recent

Author Topic: [SOLVED] Getting the Line number of a memo where Edit happens  (Read 1058 times)

seany

  • Full Member
  • ***
  • Posts: 120
[SOLVED] Getting the Line number of a memo where Edit happens
« on: January 25, 2023, 04:52:32 am »
Please consider a TMemo in a Form. Say, at any given moment, the content was

Lorem
dolor
Ipsum
sit
amit
conotis
bura


I use my mouse or the arrow keys to move to line number 4, and edit the word sit to sat.

This should trigger a Change event.

I want to capture the line number where the edit happened. I tried to look under TMemo1.Lines and TMemo1 for built in functions that could possibly return me the line number 4 as the line of edit / change. I did not find anything.

Does the Memo element have a built in method to return a line number where edit is happening? Thank you.
« Last Edit: January 27, 2023, 08:58:08 am by seany »
My projects, among others:

https://linktr.ee/siderealNight

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Getting the Line number of a memo where Edit happens
« Reply #1 on: January 25, 2023, 07:39:35 am »
As far as I know, no. You can store the original lines and test with the modified lines to find out which one has been changed.

WooBean

  • Full Member
  • ***
  • Posts: 229
Re: Getting the Line number of a memo where Edit happens
« Reply #2 on: January 25, 2023, 08:27:58 am »

I want to capture the line number where the edit happened. I tried to look under TMemo1.Lines and TMemo1 for built in functions that could possibly return me the line number 4 as the line of edit / change. I did not find anything.

Does the Memo element have a built in method to return a line number where edit is happening? Thank you.

Yes, you can use inside OnChange (for Memo1) event a call to Memo1.CaretPos.Y - this points to index of Memo1.Lines where the change just has happened.

For example
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1Change(Sender: TObject);
  2. begin
  3.   self.Label1.Caption:=inttostr(self.memo1.CaretPos.Y);
  4. end;
  5.  
shows in a Label1  the index of the Memo1.lines where change was made (0 based).

« Last Edit: January 25, 2023, 08:29:51 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Getting the Line number of a memo where Edit happens
« Reply #3 on: January 26, 2023, 12:23:21 am »

I want to capture the line number where the edit happened. I tried to look under TMemo1.Lines and TMemo1 for built in functions that could possibly return me the line number 4 as the line of edit / change. I did not find anything.

Does the Memo element have a built in method to return a line number where edit is happening? Thank you.

Yes, you can use inside OnChange (for Memo1) event a call to Memo1.CaretPos.Y - this points to index of Memo1.Lines where the change just has happened.

For example
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1Change(Sender: TObject);
  2. begin
  3.   self.Label1.Caption:=inttostr(self.memo1.CaretPos.Y);
  4. end;
  5.  
shows in a Label1  the index of the Memo1.lines where change was made (0 based).
may not work with line wrapping in windows.

maybe someone should test for that?

The only true wisdom is knowing you know nothing

WooBean

  • Full Member
  • ***
  • Posts: 229
Re: Getting the Line number of a memo where Edit happens
« Reply #4 on: January 26, 2023, 07:46:02 am »
jamie,
it was tested with WordWrap property of Memo set to TRUE (which is default).

When length of line excides avialable text line length then in cursor Y postion 1 is added, cursor shifts to next line with a new character, total count of lines increases by 1.
So, all is OK.

To test use the below:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1Change(Sender: TObject);
  2. begin
  3.   self.Label1.Caption:='Pos. line='+inttostr(self.memo1.CaretPos.Y)+
  4.    ', Lines count='+inttostr(self.memo1.lines.Count);
  5. end;
  6.  

Edited:
however there are some nuances with caret sign/symbol behaviour when we are inserting chars not at the end of text line - lines are changed as I described but caret symbol is left at the end of previous line. When we press arrow down key (on keyboard)  cursor symbol is moved to next line and not to the line begining but after first character. Such a feature?
« Last Edit: January 26, 2023, 08:50:53 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Getting the Line number of a memo where Edit happens
« Reply #5 on: January 27, 2023, 12:07:16 am »
as I commented before, it may not give you the correct line number.
With WORDWRAP on.

 If you are on the first line of the memo and you keep typing away to force it to wrap around, technically the next line is still the first line, and such the reporting index should still be 0.

  Using the Sendmessage(Memo.Handle, EM_LINEFROMCHAR, -1, 0) should return the actual line index.

  This only applies to windblows, unless something has changed since then.

The only true wisdom is knowing you know nothing

WooBean

  • Full Member
  • ***
  • Posts: 229
Re: Getting the Line number of a memo where Edit happens
« Reply #6 on: January 27, 2023, 08:55:47 am »
as I commented before, it may not give you the correct line number.
With WORDWRAP on.

 If you are on the first line of the memo and you keep typing away to force it to wrap around, technically the next line is still the first line, and such the reporting index should still be 0.

  Using the Sendmessage(Memo.Handle, EM_LINEFROMCHAR, -1, 0) should return the actual line index.

  This only applies to windblows, unless something has changed since then.

In fact, during wrapping occurence a new line is inserted in Lines property (Memo1.Count is incremented) and value returned by memo1.CaretPos.Y refers to index of line where a new wrapped beginning of text starts.
BTW, value returned by SendMessage is axactly equal to  memo1.CaretPos.Y.

If someone wants to check so he/she can use code below:

Code: Pascal  [Select][+][-]
  1. //...
  2. interface uses windows;
  3.  
  4. procedure TForm1.Memo1Change(Sender: TObject);
  5. var i:Int64; //for 32bit platform Int32;
  6. begin
  7.   i:=SendMessage(self.Memo1.Handle, EM_LINEFROMCHAR, -1, 0);
  8.   self.Label1.Caption:='Pos. line='+inttostr(self.memo1.CaretPos.Y)+
  9.    ', Lines count='+inttostr(self.memo1.lines.Count)+
  10.    ', SendMessage ret.='+inttostr(i);
  11. end;
  12.  
« Last Edit: January 27, 2023, 08:58:53 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

seany

  • Full Member
  • ***
  • Posts: 120
Re: Getting the Line number of a memo where Edit happens
« Reply #7 on: January 27, 2023, 08:57:50 am »
Thanks. I am on Linux, and the CaretPos.Y works.
My projects, among others:

https://linktr.ee/siderealNight

 

TinyPortal © 2005-2018