Solved

RichEdit - DBrichEdit

Posted on 1997-10-15
5
877 Views
Last Modified: 2013-11-22
I need to copy the rich text from a TDBRichEdit to a TRichEdit.  I mess with it in the TRichEdit (no problem there) and then I want to transfer it back to a TDBRichEdit.  I cant seem to get the rich data transferred.  I've used assign (wont let me), I've used lines.assign (no rich result) etc. etc.  I don't want to use the clipboard.

If I don't get an answer, I'm gonna take two Aspirin and jump off the couch.

0
Comment
Question by:icampbe1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 150 total points
ID: 1347644
To copy data between 2 rich edit controls, keeping the rich formatting, you need to use EM_STREAMIN and EM_STREAMOUT.  Consider the example below:

1)  You need to declare a callback function.

(Declare MemStream: TMemoryStream global;
Add RichEdit to your uses clause.)

function EdStreamCallBack(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): Longint; stdcall;
begin
    if (dwCookie = 1) then
        begin    // EM_STREAMOUT
            MemStream.SetSize(cb);
            CopyMemory(MemStream.Memory, pbBuff, cb);
            Result := 1;
        end
    else
        begin    // EM_STREAMIN
            CopyMemory(pbBuff, MemStream.Memory, MemStream.Size);
            pcb := MemStream.Size;
            Result := NOERROR;
        end;
end;

2)  The following procedure copies the text (including rich edit formatting) from "rchFrom" into "rchTo":
procedure CopyRichText(rchFrom, rchTo: TCustomRichEdit);
var
    EdStream: TEditStream;
begin
    MemStream := TMemoryStream.Create;
    FillChar(EdStream, SizeOf(TEditStream), 0);

    EdStream.dwCookie := 1;
    EdStream.pfnCallback := @EdStreamCallBack;
    rchFrom.Perform(EM_STREAMOUT, SF_RTF, Integer(@EdStream));

    EdStream.dwCookie := 2;
    MemStream.Position := 0;
    rchTo.Perform(EM_STREAMIN, SF_RTF, Integer(@EdStream));

    MemStream.Free;
end;

3)  For your purposes, assume you have a TRichEdit called rchMemo and a TDBRichEdit called rchDBEdit.  rchDBEdit is connected to Table1.
To copy the data from rchDBEdit into rchEdit:
    CopyRichText(rchDBEdit, rchEdit);

To copy the data from rchEdit back to rchDBEdit:
    Table1.Edit;
    CopyRichText(rchEdit, rchDBEdit);
    Table1.Post;

I hope this is what you are looking for.  Please note that the callback function will get called more than once for large amounts of Rich text, so you will need to modify it to read/write to MemStream multiple times.  Mail me at davekw@iafrica.com if you need it to work with large amounts of data.

JB
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1347645
P.S.  I tried the following, but couldn't get it to work...

MemStream := TMemoryStream.Create;
rchFrom.Lines.SaveToStream(MemStream);
rchTo.Lines.LoadFromStream(MemStream);
MemStream.Free;

I have no idea why this doesn't work...
0
 
LVL 2

Author Comment

by:icampbe1
ID: 1347646
I'm gonna grade you with a B.  Your answer was much too complicated but you obviously gave it some thought and work.  Your comment is much closer to the right answer.  I worked on it for a while last night and finally got it to work almost exactly like your comment using TMemoryStream.  What you're missing is Resetting the position in the stream before you load back:  MemStream.Position := 0  then it works just fine.  
Thanks again...   Ian C.

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1347647
Well done!!  You should actually get the points for figuring out the MemStream.Position := 0 thing...  Anyway, now we both know.

Glad you didn't have to jump off the couch    :-)
JB
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1347648
Oops...  I tried the following, and it copied the text but not the formatting:
    MemStream := TMemoryStream.Create;
    rchFrom.Lines.SaveToStream(MemStream);
    MemStream.Position := 0;
    rchTo.Lines.LoadFromStream(MemStream);
    MemStream.Free;

Using my "complicated" method transfers the formatting (i.e bold, italic, etc.)

JB
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question