Solved

RichEdit - DBrichEdit

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 106
Sending Gmail through Delphi 3 91
delphi parse string to params 3 97
how to change, disabled button color FMX ? 1 9
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now