Solved

RichEdit inside RIchEdit

Posted on 1998-11-04
18
227 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
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.

 
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

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
Machine not responding during CopyFile() 3 88
Delphi Form ownership 4 70
Delphi: Connect to running MS Outlook 4 47
Delphi...Split view - idea? 1 39
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…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

939 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

6 Experts available now in Live!

Get 1:1 Help Now