Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

RichEdit - DBrichEdit

Posted on 1997-10-15
5
Medium Priority
?
899 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 450 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

715 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