"Jesper Safholm" <
XXXX@XXXXX.COM >wrote:
Quote
[...] I would like to know how I put a certain text in, for
example line 4.
This question has issues that need to be discussed because
there is no absolute answer that works under all circumstances.
You first need to understand how TMemo breaks the Text property
into it's Lines. Lines break every where there is a '\n'. In
addition, lines also break according to the Width of the
ClientRect. If we assume that Text does not contain a \n and
it's TextWidth is 100 and the client width is 50, the Lines
count will be 2 or 3 depending on word length where the line
breaks.
IOW, the Lines break on whole words based on the how it would
be displayed on-screen according to it's displayable width.
Simply changing the TMemo's Width can result in changing the
number of Lines that the TMemo stores internally.
So now the primary question becomes do you really want to use
absolute Lines[x] or do you want to count the lines based on
where you find a \n and the secondary question is do you want
to insert the string into the Lines or do you want to replace
a specific Line?
How to approach this depends on your school of thought. For
example, you could disable the TMemo's painting and then
change it's Width to a bizzillian so that you *know* Lines
break at a \n, do your thing, change the Width back, re-enable
painting and Invalidate the TMemo so that it repaints with the
new Text.
While this is probably the fastest way to accomplish your goal
under all circumstances, I would opt for an approach that is
easier for others to understand what it was that I was doing.
Considering that using the absolute Lines[x] is unreliable, I
would count \n's using AnsiString::Pos and use the SelStart,
SelLength and SelText properties to get what I wanted. Here's
how the SelStart, SelLength and SelText properties work:
SelStart is the caret position with zero being to the left
of the first character in the TMemo.
SelLength is the length of the selected text. If it's
zero, no text is selected. If non-zero, it's absolute
value is the character count of the selected text. If
positive, the text to the right of the caret is selected.
If negative, the selected text is to the left of the caret.
SelText is the selected text but for this example we don't
care what the text is - just what happens when you assign a
value to SelText. If, for example, you assign the string
"Here's Johnnie!!\n" to SelText, that litteral will be
inserted into the TMemo where ever based on the value of
SelStart. If SelStart is zero, it will be inserted as the
first line (note the \n).
Now, if you want to replace the first line, set SelStart
to the beginning of the line (zero) and set SelLength to
the total number of characters in the first line including
\n and then assign the string to SelText.
Now that you know how that works, all you need to know is
how to figure out how to set SelStart and that's just as
easy using AnsiString methods. Just remember that SelStart
is zero based and AnsiString is one based. IOW, the first
character for SelStart is zero but for AnsiString it's
Text[1].
~ JD