Forum > LCL
How to get the index of the selected line in a TMemo?
MarcusM:
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: ---procedure TForm1.Button1Click(Sender: TObject);
var
line: integer;
begin
line := Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);
end;
--- End code ---
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:
EM_LINEFROMCHAR = 201; is declared on Messages.inc.
Lazarus 0.9.29 r27701 FPC 2.4.3 i386-win32-win32/win64
theo:
If you need the line where the caret is, then
Memo1.CaretPos.Y
will do.
MarcusM:
Thank you for your responses.
I tried the following but it always returns 0.
--- Code: ---procedure TForm1.Button1Click(Sender: TObject);
var
line: integer;
begin
line := Memo1.Perform(201, Memo1.SelStart, 0);
end;
--- End code ---
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: ---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;
--- End code ---
Roman:
I will not open the new topic for this question: How can I get SelStart from CaretPos?
Navigation
[0] Message Index
[#] Next page