Solved

RichEdit - DBrichEdit

Posted on 1997-10-15
5
861 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Dbf export problem to a Visual Foxpro application 6 190
URL for downloading Google Chrome for Win XP 2 158
Delphi 2 60
Delphi problems to abort a printjob 9 21
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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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