Solved

RichEdit - DBrichEdit

Posted on 1997-10-15
5
846 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

11 Experts available now in Live!

Get 1:1 Help Now