?
Solved

RichEdit inside RIchEdit

Posted on 1998-11-04
18
Medium Priority
?
234 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

765 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