Solved

RichText Control Problem

Posted on 1998-05-15
29
350 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
Comment Utility
it is very simple just PASTE it into the richedit
0
 

Author Comment

by:davidgreen
Comment Utility
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
Comment Utility
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
 

Author Comment

by:davidgreen
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ups, sorry guys, my answer above should be comment, please
reject it.
0
 
LVL 1

Expert Comment

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

Author Comment

by:davidgreen
Comment Utility
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
Comment Utility
Fire away ssite, I still haven't found a solution yet.

0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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
Comment Utility
Appreciated JimBob.... This one is driving me crazy!!

DG
horus@vmicro.com

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No way ! jimbob did the work (jumbob : you didn't push in). Let him have it ! :-)
0
 

Author Comment

by:davidgreen
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Jimbob, what david wants to do is copy from a memo, and that worked for me fine.
0
 
LVL 5

Expert Comment

by:JimBob091197
Comment Utility
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
Comment Utility
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 220 total points
Comment Utility
You leave me no choise ! Pleasure working with you, JimBon and david.

:-)
0
 

Author Comment

by:davidgreen
Comment Utility
Thanks :))))))
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

10 Experts available now in Live!

Get 1:1 Help Now