Discussion:
Inserting Lines into a Memo
(too old to reply)
S
2005-03-13 13:48:47 UTC
Permalink
I'm a Delphi newbie, so please bear with me!

I am trying to insert lines into a memo, wherever the cursor may be. That's
fine if what I'm trying to add in is only one line, but if it's more than
one line, I can't figure out how to do this.

To add one line, I do this:

Memo.Lines[Memo.CaretPos.Y] := 'Blah Blah Blah';

To add more than one line, I have tried inserting the first line using the
code above, then the other lines using:

Memo.Lines.Add('Blah Blah Blah');

But, then these lines go to the bottom of the memo (obviously).

I thought about incrementing CaretPos.Y after each line of text has been
inserted in the memo, but this is no good because firstly, it doesn't work
if more lines to not exist in the memo, and if they do, it will overwrite
them.

So I hope what I'm trying to do makes sense. So, for example, I want to
insert three lines of text wherever the cursor may be. The first line of
text can be inserted on the line of the cursor. The other two need to be
added on lines below them, but the lines need to be added so it doesn't
overwrite any other text that may already exist in the memo below it.

Many thanks in advance.
Pete
2005-03-13 15:21:06 UTC
Permalink
Post by S
I'm a Delphi newbie, so please bear with me!
I am trying to insert lines into a memo, wherever the cursor may be. That's
fine if what I'm trying to add in is only one line, but if it's more than
one line, I can't figure out how to do this.
Memo.Lines[Memo.CaretPos.Y] := 'Blah Blah Blah';
To add more than one line, I have tried inserting the first line using the
Memo.Lines.Add('Blah Blah Blah');
But, then these lines go to the bottom of the memo (obviously).
I thought about incrementing CaretPos.Y after each line of text has been
inserted in the memo, but this is no good because firstly, it doesn't work
if more lines to not exist in the memo, and if they do, it will overwrite
them.
So I hope what I'm trying to do makes sense. So, for example, I want to
insert three lines of text wherever the cursor may be. The first line of
text can be inserted on the line of the cursor. The other two need to be
added on lines below them, but the lines need to be added so it doesn't
overwrite any other text that may already exist in the memo below it.
Many thanks in advance.
Something like this? Or am I missing the point?

const
T = '12345'#13#10'ABCDE'#13#10'FGHIJ'#13#10;
var
I : integer;
S : string;
begin
with Memo1 do begin
I:= SelStart;
S:= Memo1.Text;
system.insert(T, S, SelStart + 1);
Text:= S;
SelStart:= I + length(T);
end;
end;

HTH
S
2005-03-13 18:08:10 UTC
Permalink
Post by Pete
Something like this? Or am I missing the point?
<snip>

Thanks very much for this Pete.

Yes, that works, but I just don't know if it's suitable for my application.
The reason is that T is not going to be the same every time.

Is there a simpler method of literally just inserting lines where the cursor
is positioned?
Nicholas Sherlock
2005-03-13 18:28:00 UTC
Permalink
Post by S
Post by Pete
Something like this? Or am I missing the point?
<snip>
Thanks very much for this Pete.
Yes, that works, but I just don't know if it's suitable for my application.
The reason is that T is not going to be the same every time.
Make T a string, not a constant, then stuff it with whatever you like.

Cheers,
Nicholas Sherlock
Jamie
2005-03-13 18:56:32 UTC
Permalink
Post by S
I'm a Delphi newbie, so please bear with me!
I am trying to insert lines into a memo, wherever the cursor may be. That's
fine if what I'm trying to add in is only one line, but if it's more than
one line, I can't figure out how to do this.
Memo.Lines[Memo.CaretPos.Y] := 'Blah Blah Blah';
To add more than one line, I have tried inserting the first line using the
Memo.Lines.Add('Blah Blah Blah');
But, then these lines go to the bottom of the memo (obviously).
I thought about incrementing CaretPos.Y after each line of text has been
inserted in the memo, but this is no good because firstly, it doesn't work
if more lines to not exist in the memo, and if they do, it will overwrite
them.
So I hope what I'm trying to do makes sense. So, for example, I want to
insert three lines of text wherever the cursor may be. The first line of
text can be inserted on the line of the cursor. The other two need to be
added on lines below them, but the lines need to be added so it doesn't
overwrite any other text that may already exist in the memo below it.
Many thanks in advance.
the lines property is a TstringList.
if you look at the help for that, you can
see that you have a Insert .
if you want to insert multiple lines
all at once, then make sure the <Cr><Lf> is
in a single string for the mulitple lines.
after doing this, perform an update on the memo
memo1.update;
etc.
and also a Memo1.repaint.
etc..
that should get you in a starting zone.
Heinrich Wolf
2005-03-13 19:57:23 UTC
Permalink
"S" <***@anon.com> schrieb im Newsbeitrag news:423444c0$0$18752$***@ptn-nntp-reader02.plus.net...
...
Post by S
To add more than one line, I have tried inserting the first line using the
Memo.Lines.Add('Blah Blah Blah');
But, then these lines go to the bottom of the memo (obviously).
...
What about adding not from first to last,
but reversely from last to first?
S
2005-03-13 23:14:36 UTC
Permalink
Post by Heinrich Wolf
What about adding not from first to last,
but reversely from last to first?
I'm not with you. What do you mean?
Bruce Roberts
2005-03-14 15:10:59 UTC
Permalink
Post by S
I am trying to insert lines into a memo, wherever the cursor may be. That's
fine if what I'm trying to add in is only one line, but if it's more than
one line, I can't figure out how to do this.
To add more than one line, I have tried inserting the first line using the
Memo.SelText := #13#10'An inserted line by iteself'#13#10;
S
2005-03-14 16:19:44 UTC
Permalink
Post by Bruce Roberts
Memo.SelText := #13#10'An inserted line by iteself'#13#10;
Thank you, that is perfect!

Loading...