Recent

Author Topic: Working with Tmemo...  (Read 8045 times)

Ign_17

  • New Member
  • *
  • Posts: 43
Working with Tmemo...
« on: September 12, 2015, 12:26:34 pm »
Hi everyone.

For programming reasons, I´m trying to obtain the number, or better said the index, of the string in the Tmemo.Lines string array corresponding to the first line, on the top, showed in the Tmemo Component.

For example, TStringGrid has a TopRow property that points to the first row showed on the component, and this is exactly what i´m looking for but in a Tmemo. Is there anything like this? I can´t find anything similar, and al least initially I would like to avoid working with another options like  ScrollBar Components and/or SendMessage Function.

Thank you very much for any answer.

   

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Working with Tmemo...
« Reply #1 on: September 12, 2015, 12:42:02 pm »
Code: [Select]
  showmessage(memo1.Lines[0]);
  showmessage(memo1.Lines.Strings[0]);
both end up in the same code use which ever prefer.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Working with Tmemo...
« Reply #2 on: September 12, 2015, 01:06:38 pm »
Two further points:

You need to make sure the memo is not blank (or referencing memo1.Lines[0] will give an error).

TMemo defaults to having WordWrap as True. This means Lines[0] holds the visible first line, which may well differ from the line you assigned to it initially. Try this, for instance:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  if (memo1.Lines.Count > 0) then
    ShowMessage(memo1.Lines[0]);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  memo1.Clear;
  memo1.Width:=90;
  memo1.Lines.Add('This line is too long to fit a 90 pixel memo');
end;

Ign_17

  • New Member
  • *
  • Posts: 43
Re: Working with Tmemo...
« Reply #3 on: September 12, 2015, 01:07:42 pm »
Code: [Select]
  showmessage(memo1.Lines[0]);
  showmessage(memo1.Lines.Strings[0]);
both end up in the same code use which ever prefer.

Thak you very much. I´m afraid i have not explained it well. Sorry, I write broken english  %)

What I am looking for is the number of the first string showed in the Tmemo component in the application while is running, not just the first string on the TMemo.Lines string array.

Let say, we have a Tmemo component drawed on the screen while the program is running, and we move the scroll bar up and down many times. At a certain point, what is the number of the string in Tmemo.lines showed at the top of the Tmemo component?

Thanks again.

sam707

  • Guest
Re: Working with Tmemo...
« Reply #4 on: September 12, 2015, 01:24:58 pm »
caret represents the cursor... try something around this

save the current caret position, then set the caret position at top of display (X=0, Y=0 i guess), then use these functions

Line := Memo1.CaretPos.Y;
Column := Memo1.CaretPos.X;

after what you set back the caret to the saved pos.
I dont know if it will work, it is just an idea to investigate
« Last Edit: September 12, 2015, 01:28:37 pm by sam707 »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Working with Tmemo...
« Reply #5 on: September 12, 2015, 01:45:34 pm »
Code: [Select]
uses
  LCLIntf, ......
interface

function FirstVisibleLine(const aMemo:TMemo) :Integer;

implementation

function FirstVisibleLine(const aMemo:TMemo) :Integer;
const
  EM_GETFIRSTVISIBLELINE = 206;
begin
  Result := -1;
  if Assigned(aMemo) then
    Result := SendMessage(aMemo.Handle,EM_GETFIRSTVISIBLELINE,0,0);
end;

Windows only.

Ign_17

  • New Member
  • *
  • Posts: 43
Re: Working with Tmemo...
« Reply #6 on: September 12, 2015, 02:46:48 pm »
Code: [Select]
SendMessage(aMemo.Handle,EM_GETFIRSTVISIBLELINE,0,0);


Excellent !!!!!   

I´ve tried it and works perfectly. It´s exactly what i was looking for  :D :D

Thanks so much.

sam707

  • Guest
Re: Working with Tmemo...
« Reply #7 on: September 13, 2015, 05:45:55 am »
well, i'm not sure at all that 206 Msg is crossplatform, it wont work on linux or android, I do always attempt to keep in mind crossplatform coding ;) it might be a lil bit slower at run time (who cares nowadays?) but can run on any supported device

http://www.askingbox.com/question/lazarus-get-current-line-index-from-memo

so, my way is "Excellent++" i guess :D
« Last Edit: September 13, 2015, 05:57:16 am by sam707 »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Working with Tmemo...
« Reply #8 on: September 13, 2015, 11:37:09 am »
well, i'm not sure at all that 206 Msg is crossplatform, it wont work on linux or android, I do always attempt to keep in mind crossplatform coding ;) it might be a lil bit slower at run time (who cares nowadays?) but can run on any supported device

http://www.askingbox.com/question/lazarus-get-current-line-index-from-memo

so, my way is "Excellent++" i guess :D
Not really. Your way has nothing to do with the problem at hand. You answer the question of "what is the selected line" when the question is what is the first visible line. So if you need some short of rate then you get 0 for effort and 0 for result.

 

TinyPortal © 2005-2018