Link to home
Start Free TrialLog in
Avatar of Romans
Romans

asked on

Replace Line At Cursor in RichEdit

Hello,
I am working with the RichEdit component - and I would like to be able to replace the line at the cursor - with another line. For example the RichEdit may contain

Line 1     Hello experts. I would like some help with
Line 2     some code to make my software work the
Line 3     way I need it to. I would appreciate all the
Line 4     help you your expertise has to offer.


Ok - so if my cursor is at Line 2 - I would like to replace the entire line "some code to make my software work the" with new text.

Remeber - the new text could contain some rich text codes (color etc). I am assigning this a high number of points because I would like to have some working code.

Regards
Romans
Avatar of DropZone
DropZone
Flag of United States of America image

What about the following.

      -dZ.
    // Find the current line:
    // Lines are 0-based... Though I'm not sure if CaretPos returns pixels or chars.
    Line := RichEdit1.CaretPos.Y;
 
    // If that doesn't work, try this one,
    Line := REdit.Perform(EM_LINEFROMCHAR,REdit.SelStart,0);
 
    RichEdit1.Lines[Line] := 'This is new text';

Open in new window

Avatar of Romans
Romans

ASKER

DZ,
Although this does replace the line with the new text - I do not see how this can work when there is formatting type information in the new string. In your example "This is the new text" - what happens if the word "new" needs to be bold text.

IE: The rich text with formatting codes would look like this: "This is the \b new \b0 text" where the \b and \b0 indicate the start and stop of bold text.

Thanks for the help so far!

Regards
Romans
You are right.  That won't be possible in any direct way.  The TStrings object in the Lines property does not support formatting, so the only way that I can think of doing this would be to extract the entire document, inject the new formatted line, and re-import the document somehow.

See this thread for more information:
     http://groups.google.com/group/borland.public.delphi.vcl.components.using/browse_thread/thread/9bf520d78f84b266?hl=en&ie=UTF-8&q=delphi+richedit+programmatically+adding+formatted+text#0426726813c358c5

    -dZ.
You could paste the new text (with formatting) to the clipboard, then paste it into the richedit at the cursor position.
Avatar of kretzschmar
you could also use the stream-functionality of a richedit-control .  . .
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Romans

ASKER

kretzschmar
Thanks for the input - I will look at this tonight as I have no time at the moment. I will keep you posted :)

Romans
Avatar of Romans

ASKER

kretzschmar
Still working on this - I am very close I think.

Romans
Romans, how is it? I am interested in this solution as well :)