RichText Control Problem

I have a string containing richtext code copied from notepad so it doesn't reconize it as richtext. The problem is how do I get a richedit control to recognize this as richtext again? If I just paste it into the richtext control or assign the string to the edit or lines property of the richtext control all I get is a display of the codes. Any ideas guys?
davidgreenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

saar071697Commented:
it is very simple just PASTE it into the richedit
0
davidgreenAuthor Commented:
Have you got an example of this Saar? How do I go from the string - to the clipboard - to the richtext control and have it recognized as richtext format. If I open a richtext document in notepad, copy the text, including codes and then paste into a richtext control (even wordpad) all I get is a display of codes again, it doesn't display with formatting.
I know I can easily load it via LoadFromFile but I want to be able to do it in memory... could LoadFromStream be used?
0
ssiteCommented:
Never did it, but my guess is that word, wordpad or whoever, registers a clipboard format for that.

You need to use the clipboard hasformat methos and formats array to identify this, and then copy the text buffer to the richedit.lines or to a file and the load it.

Hope this helps
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

davidgreenAuthor Commented:
I have investigated this approach but it doesn't seem to work. It seems this simple sounding problem is harder than it first apprears.

Any expert programmers out there wanting a challenge?
0
ssiteCommented:
Didn't like your reply, so I did some digging, and I was right.

Just use RichEdit1.PasteFromClipboard;  

That'll do the trick and it does just what I said.

:-)
0
davidgreenAuthor Commented:
ssites, I didn't mean to sound condescending. Say for example I have the following code in a memo control

{\rtf1\ansi\deff0\deflang1033{\fonttbl {\f0\fswiss
Arial;}}
{\colortbl ;\red0\green0\blue0;}
\uc1\pard\cf1\ulnone\f0\fs144 D\ul A\ulnone\b\i
V\b0\i0 E\fs16\par
}

If I save that to a file with a rtf extension it will load and display 'dave' complete with formatting. If I just do a copy and paste to a richtext edit control all I end up with is the above text. How do I copy it to a richedit control and display it with formatting? Did you create an example that worked?

DG
0
ssiteCommented:
David, Only now, I understand what you want to do. Look at yur questions, you never mentioned a memo control before. I'm going to look it up and let you know.
0
rene100Commented:
Hi guys

i've got a little work-around for this problem:
why don't you save the whole text (after pasting in the formatted text) in the richedit-control
as normal text and load them again as rtf-text.
(i know, this isn't so smart, but i haven't a better solution
at the moment)

regards
rene100
0
rene100Commented:
ups, sorry guys, my answer above should be comment, please
reject it.
0
ssiteCommented:
I got the answer, dave. Reject that answer and I'll post it.
0
davidgreenAuthor Commented:
Rene100 - I really need to do this is memory, the number of conversions of this sort that needs to be done would be horrid if I had to save to disk and reload.

0
davidgreenAuthor Commented:
Fire away ssite, I still haven't found a solution yet.

0
JimBob091197Commented:
Hi all,

I have done this before with EM_STREAMIN and EM_STREAMOUT, using a callback function.  I'll let ssite post first, and if the answer doesn't user EM_STREAMIN/OUT or doesn't work, I will post some code.

Cheers,
JB

0
davidgreenAuthor Commented:
Appreciated JimBob.... This one is driving me crazy!!

DG
horus@vmicro.com

0
JimBob091197Commented:
Ok, Dave, here is what I did.

Notes:
RichMemStream is a global variable that can be accessed by the callback (EdStreamCallBack).  You must get the raw text into this stream.  In my simple example I use RichMemStream.LoadFromFile, and I load a text file.

My e.g. assumes your rich edit is called rchTest.

(EdStream.dwCookie is used to tell the callback whether you are reading or writing to the stream.  It's a user-defined value...)

=== START OF CODE ===
var
  RichMemStream: TMemoryStream;

function EdStreamCallBack(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): Longint; stdcall;
begin
      if (dwCookie = 1) then
            begin                  // EM_STREAMOUT
                  RichMemStream.SetSize(cb);
                  CopyMemory(RichMemStream.Memory, pbBuff, cb);
                  Result := 1;
            end
      else
            begin            // EM_STREAMIN
                  CopyMemory(pbBuff, RichMemStream.Memory, RichMemStream.Size);
                  pcb := RichMemStream.Size;
                  Result := NOERROR;
            end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
      EdStream: TEditStream;
begin
      RichMemStream := TMemoryStream.Create;

      FillChar(EdStream, SizeOf(TEditStream), 0);

  { To get the content of a rich edit into the stream, do the following:
      EdStream.dwCookie := 1;
      EdStream.pfnCallback := @EdStreamCallBack;
      rchFrom.Perform(EM_STREAMOUT, SF_RTF, Integer(@EdStream));}

  { In your case, however, you want to put the stream's text INTO the rich edit.
  Thus, do the following: }

  // You must some how get a stream with the raw text.
  // In this simple example I use Stream.LoadFromFile...
  RichMemStream.LoadFromFile('C:\Test.txt');
      EdStream.dwCookie := 2;
  EdStream.pfnCallback := @EdStreamCallBack;
      RichMemStream.Position := 0;
      rchTest.Perform(EM_STREAMIN, SF_RTF, Integer(@EdStream));

      RichMemStream.Free;
end;
=== END OF CODE ===

Cheers,
JB
0
ssiteCommented:
I was asleep and jimbob beet me to it. Although there's a slightly easier way which is to use
richedit.lines.loadfromstream;

:-)
0
JimBob091197Commented:
Sorry, ssite, I didn't mean to push in.  ;-)

I originally used RichEdit.Lines.LoadFromStream, but I encountered problems.  This was several months ago, but I remember that I had several problems which I couldn't sort out.  Hence the EM_STREAMIN/OUT approach.

If you can give Dave Green a method with Lines.LoadFromStream he may prefer that.  I would like it too!  :-)

Cheers,
JB
0
davidgreenAuthor Commented:
Thanks JimBob, I really appreciate your help. I'm going to experiment with your code and see if this solves it.

Ssite I would be interested in seeing your approach as well.

Big question is - how do I reward you both?!

0
JimBob091197Commented:
Hehe...  the reward?  If ssite gives you code using Lines.LoadFromStream, you'll probably prefer that approach, so the points should go to ssite.

JB
0
ssiteCommented:
No way ! jimbob did the work (jumbob : you didn't push in). Let him have it ! :-)
0
davidgreenAuthor Commented:
Thanks SSite - JimBob, answer the question and I'll grade your answer.

SSite: What were your ideas on loadfromstream? Have you got a code snippet?

Thanks alot guys for your help, its appreciated.

DG.


0
ssiteCommented:
The code is extremely simple : create a TMemorystream, use savetostream from the memo and then use loadfromstream on the richedit control. should work !  But then jumbob says there're problems with this method.
0
JimBob091197Commented:
Hi all,

Before I submit an answer, I must say that the following DOES work.  (memTest is a TMemo; rchTest is a TRichEdit.)

var
  MemStr: TMemoryStream;
begin
  MemStr := TMemoryStream.Create;
  memTest.Lines.SaveToStream(MemStr);
  MemStr.Position := 0;
  rchTest.Lines.LoadFromStream(MemStr);
  MemStr.Free;
end;

(It's important to set MemStr.Position := 0.)

However, I remember having other problems when using this technique, but I can't find the sample program that I was working on to remind myself of what the problems were!

If the above code works for your needs, then ssite MUST get the points as this is by far an easier solution.  Please ssite, no more arguing, just answer the question!!  :-)

JB
0
JimBob091197Commented:
Hi again

I was playing around and I remembered what I was having problems with.  I had 2 richedits, and I was trying to copy the text of one into the other.  This worked fine if #1 was richly formatted, but not if #1 had the raw RTF text.

JB
0
ssiteCommented:
Jimbob, what david wants to do is copy from a memo, and that worked for me fine.
0
JimBob091197Commented:
Yeah, that's what I was getting at in my last 2 comments (which should have been one comment).  My problem (when I eventually found it again) was between 2 RichEdits when one of them had the raw text with all the control characters.

JB
0
davidgreenAuthor Commented:
GREAT! JimBob... the above does work... and is much simpler than the other code. Thank you JB and SSite... I wish I had 1,000,000 points to give you both.

SSite... JB wants you to have the points.. let's put this to bed.

THANKS AGAIN....
0
ssiteCommented:
You leave me no choise ! Pleasure working with you, JimBon and david.

:-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davidgreenAuthor Commented:
Thanks :))))))
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.