How can I add text not to a new line but to the last existing line? Lines.Add and Lines.Append add text as a new line, and Lines.Insert needs a position that I don't know how to find.
Asked
Active
Viewed 8,191 times
3 Answers
3
You can use the last line itself, or the entire content:
// RE = TRichEdit, Temp = string;
// Last line only
Temp := RE.Lines[RE.Lines.Count - 1];
Temp := Temp + ' plus some new text';
RE.Lines[RE.Lines.Count - 1] := Temp;
// The entire content
Temp := RE.Text;
Temp := Temp + ' plus some new text';
RE.Text := Temp;
Note that the first way is better, especially when the RichEdit contains a large amount of text. Reading and writing to RichEdit.Text can involve moving lots of text around in memory.
EDIT: After the OP's comment to my answer:
To format the text, save SelStart before adding, and then use SelLength and SelAttributes to apply formatting:
// StarPos and Len are both Integers.
StartPos := Length(RE.Text);
Len := Length(YourNewTextToBeAdded);
// Do stuff here to add text
RE.SelStart := StartPos;
RE.SelLength := Len;
RE.SelAttributes.Style := RE.SelAttributes.Style + [fsBold];

Ken White
- 123,280
- 14
- 225
- 444
-
I tried it and it worked. Thanks a lot. But now how can I format this added text. – Azad Salahli Dec 31 '09 at 19:28
1
You can use "Strings" and "Count" properties.
RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]:=RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]+'Text';

bmeric
- 1,124
- 1
- 17
- 27
-
Lines.Strings[] and Lines[] are the same thing; Lines is a default property which reads and writes Lines.Strings. – Ken White Dec 31 '09 at 18:42
1
Perform
with RichEdit1 do
begin
Lines.Add(s);
Perform( EM_SCROLL, SB_LINEDOWN, 0);
end;

k.bon
- 386
- 2
- 9