Solved

RichEdit inside RIchEdit

Posted on 1998-11-04
18
226 Views
Last Modified: 2013-11-22
I have a RichEdit1 component with some text in it, and I want to insert in some position of it the contents of another RichEdit2. If I use Streams and LoadFromStream, then all text in RichEdit1 gets overwritten by text in RichEdit2. And if I use RichEdit.Seltext = Richedit2.Seltext then I get RichEdit2's text, but without format identifiers as color, bold, underline. etc.

So my question is:

How can I put a Stream inside another Stream in order to put A Richedit2's text inside another richedit1's text without loosing format information?

Thanks ver much.
0
Comment
Question by:quique
  • 9
  • 6
  • 2
  • +1
18 Comments
 
LVL 1

Expert Comment

by:jecksom
ID: 1345561
Hi quique!

Just save to stream richedit1 then save richedit2 to save stream  and then load from
stream richedit1 , is that solution ?

Best Regards ,
             Jecksom

0
 
LVL 1

Expert Comment

by:jecksom
ID: 1345562
sorry,  "to save stream" = to same stream" :)

0
 
LVL 1

Expert Comment

by:jecksom
ID: 1345563
Hi quiue!

Hm , it's not so easy as i tough before, after:

richedit1.Lines.SaveToStream(stream1);
richedit1.Lines.SaveToStream(stream2);
stream2.position:=stream2.position-1;
stream1.copyfrom(stream2,stream2.size);
stream1.position:=0;
memo1.lines.loadfromstream(stream1);

then in memo1 you'll get nice piece of two RTF-format fragments.
Here you can start parse them ! ;)

Best Regards ,
             Jecksom.

0
 
LVL 2

Expert Comment

by:javiertb
ID: 1345564
I tried that code and didn't work. I got a 'Stream read error' at line stream1.copyfrom(stream2, stream2.size)
0
 
LVL 1

Author Comment

by:quique
ID: 1345565
jecksom, look at javiertb's comment to see if you can solve it
0
 
LVL 1

Expert Comment

by:jecksom
ID: 1345566
procedure TForm1.Button1Click(Sender: TObject);
var
stream1:tmemorystream;
stream2:tmemorystream;
begin
stream1:=tmemorystream.create;
stream2:=tmemorystream.create;
richedit1.Lines.LoadFromFile('c:\test.rtf'); //my RTF file
richedit1.Lines.SaveToStream(stream1);
richedit1.Lines.SaveToStream(stream2);
stream1.position:=stream1.position-1;
stream2.Position:=0;
stream1.copyfrom(stream2,stream2.size);
stream1.position:=0;
memo1.lines.loadfromstream(stream1);
stream1.destroy;
stream2.destroy;
end;

Sorry , just place richedit ,memo and button on form and then link onbutton click with this event.

Jecksom

0
 
LVL 1

Author Comment

by:quique
ID: 1345567
No way. I also tried that, but it seems that I can only copy from one richedit to another but not to insert.
I'll explain better the background:

I have a little text editor with a RichEdit component. Then I have an option in this editor to insert templates. These templates are stored in a FormattedField (for the richedit component) of a table. What I'm trying to do is to insert this template right in the cursor position of the text in the editor.
Hope this helps a little more.

0
 
LVL 1

Expert Comment

by:jecksom
ID: 1345568
Hi quique!

Hm , maybe OLE automation would be answer ? Weakness of this , you must have
MSWord installed ...

Jecksom

0
 
LVL 2

Expert Comment

by:Thaddy
ID: 1345569
Why not use the clipboard?
Richedit1.CopyToClipBoard
Richedit2.pastefromclipboard

Delphi does all the streaming for you....
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:jecksom
ID: 1345570
Thaddy:

you right:

procedure TForm1.Button1Click(Sender: TObject);
begin
richedit1.Lines.LoadFromFile('c:\test1.rtf');
richedit1.SelectAll;
richedit1.CopyToClipboard;
richedit2.PasteFromClipboard;
end;

Works , i've tryed this before , but didn't get success , was little sleeping i guess :)

Jecksom

0
 
LVL 2

Expert Comment

by:javiertb
ID: 1345571
The problem is that quique (I'm a friend of him) doesn't wan't to use the clipboard (maybe the user has something in it) and doesn't have msword installed, nor the final users. So the only way is streaming, but he can't get to make it work.
0
 
LVL 1

Author Comment

by:quique
ID: 1345572
Look at javiertb comment
0
 
LVL 1

Expert Comment

by:jecksom
ID: 1345573
Streaming only , well , i have no idea right now , i'll let you know if  i'll get any success.
btw , you always can presave and then restore back buffer before merging two richedits.

Jecksom

0
 
LVL 1

Expert Comment

by:jecksom
ID: 1345574
i'm talking about clipboard buffer saving and restoring.

0
 
LVL 1

Author Comment

by:quique
ID: 1345575
I don't want to use the clipboard. I want to use Streams!
0
 
LVL 1

Author Comment

by:quique
ID: 1345576
Well, I desist of streaming, but another problem is how can I copy a dbrichedit to the clipboard? because the TableField.CopyToClipboard doesn't work.
0
 
LVL 1

Accepted Solution

by:
jecksom earned 100 total points
ID: 1345577
Hi quique!

here is what you want using streaming :

Type
  TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;

  TEditStream = record
    dwCookie: Longint;
    dwError: Longint;
    pfnCallback: TEditStreamCallBack;
  end;

function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
 cb: Longint; var pcb: Longint): Longint; Stdcall;
var
  theStream: TStream;
  dataAvail: LongInt;
begin
  theStream := TStream(dwCookie);
  with theStream do begin
    dataAvail := Size - Position;
    Result := 0;
    if dataAvail <= cb then begin
      pcb := Read(pbBuff^, dataAvail);
      if pcb <> dataAvail then
        result := E_FAIL;
    end
    else begin
      pcb := Read(pbBuff^, cb);
      if pcb <> cb then
        result := E_FAIL;
    end;
  end;
end;


Function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;
 var
   theStream: TStream;
 begin
   theStream := TStream(dwCookie);

   with theStream do begin
     If cb > 0 Then
       pcb := Write(pbBuff^, cb);
     Result := 0;
   end;
 end;

procedure TForm1.Button2Click(Sender: TObject);
Var
  st: TMemoryStream;
  editstream: TEditStream;
begin
  richedit1.lines.loadfromfile('c:\test1.rtf');
  st := TMemoryStream.Create;
  try
  editstream.dwCookie:= Longint(st);
  editstream.dwError:= 0;
  editstream.pfnCallback:= EditStreamOutCallBack;
  richedit1.selectall;
  Richedit1.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION,
                     longint(@editstream));
  st.Position := 0;
  editstream.dwCookie:= Longint(st);
  editstream.dwError:= 0;
  editstream.pfnCallback:= EditStreamInCallBack;
  Richedit2.Perform( EM_STREAMIN, SF_RTF or SFF_SELECTION,
                     longint(@editstream));
  finally
    st.Free;
  end;
end;

Place 2 richedits and one button , don't forget uses richedit and comctrls.

Best Regards,
Jecksom
0
 
LVL 1

Author Comment

by:quique
ID: 1345578
Thanks very much, it works fine!
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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

22 Experts available now in Live!

Get 1:1 Help Now