Board index » delphi » TMemo: Which lines am I on?

TMemo: Which lines am I on?

TMemo: Which line am I on?

Using a popup menu, I want to add text to a line in a TMemo. (I'm
actually adding html code.) I could do this by typing:

-------------------------------------------------------
Memo1.Lines[0] := Memo1.Lines[0]+'<BR>';
-------------------------------------------------------

The problem is that the user may not actually be on line #1.

For fun I have also tried:

-------------------------------------------------------
procedure TForm1.LineBreakbr1Click(Sender: TObject);
var
  i : integer;
begin
for i := 0 to Memo1.Lines.Count - 1 do
Memo1.Lines[i] := Memo1.Lines[i]+'<BR>';
end;
-------------------------------------------------------

But of course this adds '<BR>' to the end of every line.

Is there a way of reading JUST the line the user is presently on and
setting [n] so the text is just printed JUST on that line?

Memo1.Lines[n] := Memo1.Lines[n]+'<BR>';

Much appreciated!
Thanks
Nathalie

 

Re:TMemo: Which lines am I on?


Quote
> Of course there is a way. The property to use is the CaretPos property of
> the TMemo object. If all goes well it returns the position of the cursor on
> the memo. With this you could find the line it is on or use the insert
> function of the TStrings object the property Lines of TMemo is made of.

Well - I've done this so far ... Using a popup menu, I am adding text on
the current line. But it doesn't add in hte text in the exact position
where the cursor is placed.

Can you show me an example of what you're talking about?

Thanks

Nat
----------------------------------------------------------------
procedure TForm1.SomethingClick(Sender: TObject);
var
  iLine : Integer ;
begin
   iLine := Memo1.Perform(em_LineFromChar, $FFFF, 0);
   Memo1.Lines[iLine] := Memo1.Lines[iLine]+'Typing Text'
end;

Re:TMemo: Which lines am I on?


Nathalie,

Of course there is a way. The property to use is the CaretPos property of
the TMemo object. If all goes well it returns the position of the cursor on
the memo. With this you could find the line it is on or use the insert
function of the TStrings object the property Lines of TMemo is made of.

Hopefully you have gained some answers,

Bram Bonsen

Quote
-----Original Message-----
From: Nathalie <NOSPAM.lech...@NOSPAM.ntic.qc.ca>
Newsgroups: alt.comp.lang.borland-delphi
Date: dinsdag 29 september 1998 17:43
Subject: TMemo: Which lines am I on?

>TMemo: Which line am I on?

>Using a popup menu, I want to add text to a line in a TMemo. (I'm
>actually adding html code.) I could do this by typing:

>-------------------------------------------------------
>Memo1.Lines[0] := Memo1.Lines[0]+'<BR>';
>-------------------------------------------------------

>The problem is that the user may not actually be on line #1.

>For fun I have also tried:

>-------------------------------------------------------
>procedure TForm1.LineBreakbr1Click(Sender: TObject);
>var
>  i : integer;
>begin
>for i := 0 to Memo1.Lines.Count - 1 do
>Memo1.Lines[i] := Memo1.Lines[i]+'<BR>';
>end;
>-------------------------------------------------------

>But of course this adds '<BR>' to the end of every line.

>Is there a way of reading JUST the line the user is presently on and
>setting [n] so the text is just printed JUST on that line?

>Memo1.Lines[n] := Memo1.Lines[n]+'<BR>';

>Much appreciated!
>Thanks
>Nathalie

Re:TMemo: Which lines am I on?


In article <361189D0....@NOSPAM.ntic.qc.ca>, Nathalie

Quote
<NOSPAM.lech...@NOSPAM.ntic.qc.ca> writes:
>Well - I've done this so far ... Using a popup menu, I am adding text on
>the current line. But it doesn't add in hte text in the exact position
>where the cursor is placed.

You've got to delete the old line and insert the new line as :-

procedure TForm1.Button1Click(Sender: TObject);
var
 LnPos, ChPos : integer;
 OldLine, NewLine : string;
begin
  {get line number and character position in line}
  LnPos := Memo1.Perform(EM_LINEFROMCHAR, -1, 0);
  ChPos := Memo1.SelStart - Memo1.Perform(EM_LINEINDEX, LnPos, 0);
  {get line to change}
  OldLine := Memo1.Lines[LnPos];
  {insert text from edit into oldline to make new line}
  NewLine := Copy(OldLine, 1, ChPos - 1) + Edit1.Text + Copy(OldLine, ChPos,
Length(OldLine));
  {remove old line}
  Memo1.Lines.Delete(LnPos);
  {insert new line}
  Memo1.Lines.Insert(LnPos, NewLine);
end;

This was with a TMemo, a TEdit, and a TButton on a form. Enter text in the
edit, position the cursor in the memo, and click the button.

Alan Lloyd
alangll...@aol.com

Re:TMemo: Which lines am I on?


Maybe our ColorMemo could help you ...

Gintaras Pikelis
Baltic Solutions
http://www.balticsolutions.com

Other Threads