views:

412

answers:

2

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.

+1  A: 

You can use "Strings" and "Count" properties.

RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]:=RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]+'Text';

barism
Lines.Strings[] and Lines[] are the same thing; Lines is a default property which reads and writes Lines.Strings.
Ken White
+1  A: 

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
I tried it and it worked. Thanks a lot. But now how can I format this added text.
Azad Salahli