Recent

Author Topic: How to get the index of the selected line in a TMemo?  (Read 35924 times)

MarcusM

  • New Member
  • *
  • Posts: 12
How to get the index of the selected line in a TMemo?
« on: October 20, 2010, 09:09:31 pm »
I would like to retrieve the index of the selected line of a TMemo so that I can act on the object that is associated with the line. I have searched the internet and have not found anything definitive. I did find the following Borland C++ Builder example to which I have converted to FPC. (source: http://www.delphigroups.info/3/1/145500.html)

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  line: integer;
begin
  line := Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);
end;

I get the following compilation error:
unit1.pas(36,40) Error: Identifier not found "EM_LINEFROMCHAR"

Some has posted a message on this forum concerning EM_LINEFROMCHAR saying it is not available on Linux. I use Ubuntu. Is this correct?
http://www.lazarus.freepascal.org/index.php?topic=5014.0

a related Delphi link is:
http://delphi.about.com/od/adptips2005/qt/memoselectline.htm

Does anyone know how this can be achieved?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to get the index of the selected line in a TMemo?
« Reply #1 on: October 20, 2010, 09:32:51 pm »
EM_LINEFROMCHAR = 201; is declared on Messages.inc.

Lazarus 0.9.29 r27701 FPC 2.4.3 i386-win32-win32/win64

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1931
Re: How to get the index of the selected line in a TMemo?
« Reply #2 on: October 20, 2010, 09:38:25 pm »
If you need the line where the caret is, then

Memo1.CaretPos.Y

will do.

MarcusM

  • New Member
  • *
  • Posts: 12
Re: How to get the index of the selected line in a TMemo?
« Reply #3 on: October 23, 2010, 06:33:52 pm »
Thank you for your responses.

I tried the following but it always returns 0.

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  line: integer;
begin
  line := Memo1.Perform(201, Memo1.SelStart, 0);
end;

So, I wrote the following function that tries to determine the first and last index of lines that are selected. However, it only works if the user highlights text moving forward not backward. One thing to note, it is implemented to return the index of the caret when no line is selected - something I also want but did not see a need to communicate in the OP. Any better ideas?

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  start,
  finish: integer;

begin
GetLinesIndex(Memo1, start, finish);
ShowMessage(IntToStr(start) + ' - ' + IntToStr(finish));
end;

procedure TForm1.GetLinesIndex(memo: TMemo; var firstIndex: integer; var lastIndex: integer);
var
tf: boolean;

index,
len: integer;

strAtLastIndex: string;

begin
firstIndex := 0;
lastIndex := memo.CaretPos.Y;
strAtLastIndex := memo.Lines[lastIndex];

if (memo.SelLength = 0) then begin
{nothing selected}
firstIndex := lastIndex;
end else if ((memo.SelLength = Length(strAtLastIndex)) and (memo.SelText = strAtLastIndex)) then begin
{whole line selected}
firstIndex := lastIndex;
end else if (memo.CaretPos.X - memo.SelLength < 0) then begin
{
multiple lines selected - presuming selection made where X is at end of the selected text not at the beginning.
thus, user will have highlighted text moving forward not backwards.
sigh, how else to do it?
}
tf := true;
len := memo.SelLength - memo.CaretPos.X;
index := lastIndex;
while (tf) do begin
index := index - 1;
if ((index <= 0) or (len < Length(memo.Lines[index]))) then begin
tf := false
end else
len := len - Length(memo.Lines[index]);
end;
firstIndex := index;
end else if (memo.SelLength < Length(strAtLastIndex)) then begin
{partial line selected}
firstIndex := lastIndex;
end else begin
{this means error}
firstIndex := -1;
lastIndex := -1;
end;
end;


Roman

  • New Member
  • *
  • Posts: 25
Re: How to get the index of the selected line in a TMemo?
« Reply #4 on: December 19, 2010, 11:30:44 am »
I will not open the new topic for this question: How can I get SelStart from CaretPos?
« Last Edit: December 19, 2010, 12:16:07 pm by Roman »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to get the index of the selected line in a TMemo?
« Reply #5 on: December 19, 2010, 12:52:31 pm »
Code: [Select]
Memo1.SelStart; 

Roman

  • New Member
  • *
  • Posts: 25
Re: How to get the index of the selected line in a TMemo?
« Reply #6 on: December 19, 2010, 01:32:52 pm »
Code: [Select]
Memo1.SelStart; 
This works if the text is already selected. My goal is the just the opposite: to select a text starting at caret position.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to get the index of the selected line in a TMemo?
« Reply #7 on: December 19, 2010, 01:38:28 pm »
Code: [Select]
  Memo1.SetFocus;
  Memo1.SelLength := 2;

Roman

  • New Member
  • *
  • Posts: 25
Re: How to get the index of the selected line in a TMemo?
« Reply #8 on: December 19, 2010, 02:01:07 pm »
Of course, thank you.

 

TinyPortal © 2005-2018