Board index » cppbuilder » Transfering Richedits to another RichEdit.

Transfering Richedits to another RichEdit.

I need to be able to transfer the 'formatted' contents of a couple of
RichEdits into another RichEdit, the best I can come-up with is:

   RichEdit1->SelectAll();
   RichEdit1->CopyToClipboard();
   RichEdit3->PasteFromClipboard();
   RichEdit2->SelectAll();
   RichEdit2->CopyToClipboard();
   RichEdit3->PasteFromClipboard();

But that seems really, really 'hackish' is there a better way, using
TMemoryStream(s) or something.  I've tried, but I just can't seem to get
it.  Thanks.

George

 

Re:Transfering Richedits to another RichEdit.


Try using the SaveToStream method (untested):

TMemoryStream *Strm = new TMemoryStream;
RichEdit1->Lines->SaveToStream(Strm);
RichEdit2->Lines->SaveToStream(Strm);
Strm->Position = 0;
RichEdit3->Lines->LoadFromStream(Strm);
delete Strm;

Gambit

Quote
George Boutwell <gbout...@yahoo.com> wrote in message

news:39F5F9BA.749C04B0@yahoo.com...
Quote
> I need to be able to transfer the 'formatted' contents of a couple of
> RichEdits into another RichEdit, the best I can come-up with is:

>    RichEdit1->SelectAll();
>    RichEdit1->CopyToClipboard();
>    RichEdit3->PasteFromClipboard();
>    RichEdit2->SelectAll();
>    RichEdit2->CopyToClipboard();
>    RichEdit3->PasteFromClipboard();

Re:Transfering Richedits to another RichEdit.


Quote
Remy Lebeau wrote:
> Try using the SaveToStream method (untested):

> TMemoryStream *Strm = new TMemoryStream;
> RichEdit1->Lines->SaveToStream(Strm);
> RichEdit2->Lines->SaveToStream(Strm);
> Strm->Position = 0;
> RichEdit3->Lines->LoadFromStream(Strm);
> delete Strm;

Actually I tried that along with some other variants where I used two,
then three TMemoryStream in an attempt to get it working.  It either
only copied one of the TRichEdit's 'formatted' text, or none.  Maybe I
did something wrong.  Here's my last attempt that  'failed', maybe you
can see something wrong with it:

   TMemoryStream* Buffer1=new TMemoryStream(),
                * Buffer2=new TMemoryStream();

   int CombinedSize = (RichEdit1->GetTextLen() +
RichEdit2->GetTextLen()) + 1;

   Buffer1->SetSize(CombinedSize);
   RichEdit1->Lines->SaveToStream(Buffer1);
   Buffer2->SetSize(RichEdit2->GetTextLen());
   RichEdit2->Lines->SaveToStream(Buffer2);

   Buffer2->Position = 0;
//   Buffer1->Seek(RichEdit1->GetTextLen(), soFromBeginning);
//   Buffer1->Write(Buffer2, Buffer2->Size);
   Buffer1->CopyFrom(Buffer2, Buffer2->Size);

   Buffer1->Position = 0;
   RichEdit3->Lines->LoadFromStream(Buffer1);

//   Buffer2->Position = 0;
//   RichEdit3->Lines->LoadFromStream(Buffer2);
   delete Buffer2;
   delete Buffer1;

As you can see... I've tried quite a few things with TMemoryStream and
not gotten to work.  Maybe you see something that is 'blaringly' wrong.

Thanks.

George

Re:Transfering Richedits to another RichEdit.


I don't see anything right off that could be wrong, but I do see that when
you call CopyFrom() you can pass a 0 and it'll ready the entire stream,
rather than you having to set Position yourself first, ie:

    Buffer1->CopyFrom(Buffer2, 0);
But other than that, I don't see anything else without doing tests myself on
the code, which I can't do at the moment.

Gambit

Quote
George Boutwell <gbout...@yahoo.com> wrote in message

news:39F6E301.A1C88AC7@yahoo.com...
Quote
> Actually I tried that along with some other variants where I used two,
> then three TMemoryStream in an attempt to get it working.  It either
> only copied one of the TRichEdit's 'formatted' text, or none.  Maybe I
> did something wrong.  Here's my last attempt that  'failed', maybe you
> can see something wrong with it:

Re:Transfering Richedits to another RichEdit.


In <39F5F9BA.749C0...@yahoo.com>, George Boutwell wrote:

Quote
> I need to be able to transfer the 'formatted' contents of a couple of
> RichEdits into another RichEdit, the best I can come-up with is:
>    RichEdit1->SelectAll();
>    RichEdit1->CopyToClipboard();
>    RichEdit3->PasteFromClipboard();
>    RichEdit2->SelectAll();
>    RichEdit2->CopyToClipboard();
>    RichEdit3->PasteFromClipboard();

How do you get that to transfer the *formatted* contents? Whenever I've
tried it works exactly as the docs say it will - it goes onto the
clipboard as CF_TEXT, i.e., the RTF is stripped.

        -- AK

--
adam.kip...@pobox.com
PGP keys available from servers

Re:Transfering Richedits to another RichEdit.


I'll try this... Thank you...
Quote
Remy Lebeau wrote:
> But other than that, I don't see anything else without doing tests myself on
> the code, which I can't do at the moment.

Re:Transfering Richedits to another RichEdit.


Well... That didn't work... But I got this to work.  The problem was not
in TMemoryStream, but in RichEdit and the RTF format.  RichEdit1's
'buffer' held a 'complete RTF file' and so RichEdit3 was only displaying
RichEdit1's buffer.  After you remove the 'RTF EOF', and the 'RTF BOF'
markers from RichEdit1 & RichEdit2 (respectively) it works just dandy.

I can explain away and only confuse you, here's some code that'll clear
things up (I hope to put some of this into CodeCentral soon):

   int   nRTFFooterSize = 4,
         nRTFHeaderSize = 6;

   TMemoryStream* Buffer1=new TMemoryStream(),
                * Buffer2=new TMemoryStream();

   Buffer1->SetSize(1);
   RichEdit1->Lines->SaveToStream(Buffer1);
   Buffer2->SetSize(1);
   RichEdit2->Lines->SaveToStream(Buffer2);

   // Append the two RichEdits
   int nPos = Buffer1->Seek((Buffer1->Size - nRTFFooterSize),
                            soFromBeginning);
   Buffer2->Position = nRTFHeaderSize;
   Buffer1->CopyFrom(Buffer2, (Buffer2->Size - nRTFHeaderSize));

   Buffer1->Position = 0;
   RichEdit3->Lines->LoadFromStream(Buffer1);

   delete Buffer2;
   delete Buffer1;

HTH,

George

Quote
George Boutwell wrote:
> I'll try this... Thank you...

Other Threads