Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

RichText Control Problem

Posted on 1998-05-15
29
Medium Priority
?
397 Views
Last Modified: 2010-04-06
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?
0
Comment
Question by:davidgreen
  • 10
  • 9
  • 7
  • +2
29 Comments
 
LVL 2

Expert Comment

by:saar071697
ID: 1345959
it is very simple just PASTE it into the richedit
0
 

Author Comment

by:davidgreen
ID: 1345960
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
 
LVL 1

Expert Comment

by:ssite
ID: 1345961
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:davidgreen
ID: 1345962
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
 
LVL 1

Expert Comment

by:ssite
ID: 1345963
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
 

Author Comment

by:davidgreen
ID: 1345964
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
 
LVL 1

Expert Comment

by:ssite
ID: 1345965
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
 
LVL 2

Expert Comment

by:rene100
ID: 1345966
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
 
LVL 2

Expert Comment

by:rene100
ID: 1345967
ups, sorry guys, my answer above should be comment, please
reject it.
0
 
LVL 1

Expert Comment

by:ssite
ID: 1345968
I got the answer, dave. Reject that answer and I'll post it.
0
 

Author Comment

by:davidgreen
ID: 1345969
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
 

Author Comment

by:davidgreen
ID: 1345970
Fire away ssite, I still haven't found a solution yet.

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345971
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
 

Author Comment

by:davidgreen
ID: 1345972
Appreciated JimBob.... This one is driving me crazy!!

DG
horus@vmicro.com

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345973
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
 
LVL 1

Expert Comment

by:ssite
ID: 1345974
I was asleep and jimbob beet me to it. Although there's a slightly easier way which is to use
richedit.lines.loadfromstream;

:-)
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345975
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
 

Author Comment

by:davidgreen
ID: 1345976
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
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345977
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
 
LVL 1

Expert Comment

by:ssite
ID: 1345978
No way ! jimbob did the work (jumbob : you didn't push in). Let him have it ! :-)
0
 

Author Comment

by:davidgreen
ID: 1345979
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
 
LVL 1

Expert Comment

by:ssite
ID: 1345980
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
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345981
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
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345982
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
 
LVL 1

Expert Comment

by:ssite
ID: 1345983
Jimbob, what david wants to do is copy from a memo, and that worked for me fine.
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345984
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
 

Author Comment

by:davidgreen
ID: 1345985
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
 
LVL 1

Accepted Solution

by:
ssite earned 880 total points
ID: 1345986
You leave me no choise ! Pleasure working with you, JimBon and david.

:-)
0
 

Author Comment

by:davidgreen
ID: 1345987
Thanks :))))))
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

885 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