Link to home
Start Free TrialLog in
Avatar of shiloh_state
shiloh_state

asked on

Moving font styles from one RichEdit to another

I'm completely desperate here, I've been working on this stupid problem for over 6 months now and the deadline is coming up quick.


First off, I'm using BCB5. I have 2 RichEdits, one to type text, and one to display the result. In the one that the user can type in, they can add bold and italics, underline, etc.

for example (bear with me, please) a message in the RichEdit1 could look like:

enter <bold>cyprus106</bold> get user <italics>stuff</italics>


The user sends the data to my parser, and then the second RichEdit displays it. It has the same text, but all of the bold, italics and underlining is  gone. I have to have those font styles.


I know I can use streams to keep the text...
I can use:
RichEdit1->Lines->SaveToStream(MyStream);
But I CANNOT use:
RichEdit2->Lines->LoadFromFile(MyStream);

I have to keep the previous messages that the user sent. When RichEdit2 loads from the stream, it clears out all of the previous messages. I can't have this.


If at all possible, I would prefer not to use streams.
I can't save to and load from a file. We tried that, We DEFINATELY can't use it.
And I can't cut/copy to and paste from the clipboard. I have users that cut commands and paste them later. Saving to the clipboard will clear their pastes. That's a bad idea.

I'm completely out of ideas and incredibly frusterated. I cannot find anything anywhere about how to do this and I HAVE to have it done. I'm at my witts end. Can ANYBODY help me >> ?
ASKER CERTIFIED SOLUTION
Avatar of DrDelphi
DrDelphi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shiloh_state
shiloh_state

ASKER

Sorry, I was wrong... I NEED to use Streams. But I can't do a LoadFromStream on the RichEdit2.
I have to convert the stream to text and add it to the RichEdit. So, basically I need to convert a TStream or TMemoryStream to text. Any suggestions?

By the way, DrDelphi, I implemented your code and couldn't get it to work. I know how to program in both delphi and builder. Converting the code wasn't the problem. It just wasn't functional. It doesn't really matter, however, as I need the code for a Stream. Sorry for wasting your time.
shiloh,
  I should have mentionned that the code I gave you was implemented in the OnMouseUp event of RichEdit1 after text has been selected. Try it, you'll see it works. Meanwhile, I am working on a BCB example for you.


Good luck!!
Okay, BCB example for ya:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  RichEdit1->Lines->LoadFromFile("Z:\\overview.rtf");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TMemoryStream *mem=new TMemoryStream;
  RichEdit1->Lines->SaveToStream(mem);
  mem->Seek(0,0); ///VERY IMPORTANT!!
  TStringList *newtext=new TStringList;
  newtext->LoadFromStream(mem);
  RichEdit2->Lines->Append(newtext->Text);
  //clean up resources!!
}
//---------------------------------------------------------------------------


Good luck!!


well, how 'bout that, it did it. But there was just one problem, it gives me an exception, "RichEdit line insertion error" I did a try-catch around it, that stopped the dialog, but why is it doing it and how do I fix it? I'm not fond of errors.
It has something to do with the declaration of AnsiString in BCB, so far I have not figured out what exactly. I have seen this error come up a lot, but to date it has never done any harm. I have just done what you did... encase the code in a Try...Catch block. I have an email in with Borland about this because of some Delphi stuff that I am porting over to BCB which has demonstrated this same error in BCB (not Delphi).


Good luck!!
Ooops.... I misspoke. The Delphi code I have DOES show that same error, too. I confused it with the first example I showed you, which was originally going to be my workaround until I discovered that the insertion error wasn't causing any problems. Long story short: The first code I mentioned works, but tends to be a little slow since you are copying the contents of RichEdit1 to RichEdit2 one character at a time. The second example works a charm, but displays an error message that doesn't seem to have any ill effect (and which can be circumvented by try..catch). Hope this clears it up for you. In either case,you're a LOT closer now to a solution than 6 months ago. <g>


Good luck!!
yup. It works... and with streams. just what I needed... BUT I have a problem. I'm sending the streams across sockets, (this is a commercial product that I'm adding functionality to, etc...) That part is covered. No internet questions here, but it sends the message to the server, which reads the text and sends it wherever accordingly. Sending a stream is a bit different. I need to be able to read that stream. Basically, I need to convert the stream to an AnsiString. I've no idea how to do this and all the code I've tried dies somewhat miserably.

do you have Anything? I found some delphi code a while back to do this. You seem like the person that can convert it. If you would or could, I could stick some closure to this statement and get it over with. IF you can't that's alright too!

Cyprus
Hmmm... once again I am at the office and nowhere near a BCB machine. I can tell you though that in Delphi I'd typecast the TMemoryStream's Memory to PChar.No reason to think that BCB will be different. I can test it out later when I get home for you... in the meanwhile, here's the Delphi equivalent.




var msg:AnsiString;
begin
   msg:=PChar(mem.Memory);
   ShowMessage(msg);
end;


Good luck!!


alright, thank you. This was the code I pulled off of the Borland Community website a while back. Anything will do, as long as I can get it converted to a String. Thanks.

procedure TForm1.Button1Click(Sender: TObject);
var
  SourceString: string;
  MemoryStream: TMemoryStream;
begin
  SourceString := 'Hello, how are you doing today?';
  MemoryStream := TMemoryStream.Create;
  try
    // Write the string to the stream. We want to write from SourceString's
    // data, starting at a pointer to SourceString (this returns a pointer to
    // the first character), dereferenced (this gives the actual character).
    MemoryStream.WriteBuffer(Pointer(SourceString)^, Length(SourceString));
    // Go back to the start of the stream
    MemoryStream.Position := 0;
    // Read it back in to make sure it worked.
    SourceString := 'I am doing just fine!';
    // Set the length, so we have space to read into
    SetLength(SourceString, MemoryStream.Size);
    MemoryStream.ReadBuffer(Pointer(SourceString)^, MemoryStream.Size);
    Caption := SourceString;
  finally
    MemoryStream.Free;
  end;
end;
Dear shiloh_state

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "DrDelphi"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: https://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
cyprus106 AND shiloh_state ??? Only one account allowed.... please let us know more about this and any other accounts you may have here so we can help you close all but one (unless I am mistaken here).

If I have misunderstood this situation or misinterpreted the response here, please let me know.

https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
https://www.experts-exchange.com/jsp/infoMemberAgreement.jsp

Thank you,
Moondancer - EE Moderator
i apologize. One is my friends account. I had him send a reply when I forgot my password.
Lets see, You forgot the password in an hour and 53 minutes, and even signed one of your comments from shiloh_state as cyprus.  I would strongly suggest which account you wish to keep.

ComTech
CS Admin @ EE
What are you implying? We're friends. We work in the same office and we have autologin to your site on a few of our computers, therfore when either one of us pops up this page, it is automatically logged in as whomever told it to log in first... I have autologin on a few, he has autologin on a few. Depending on which computer either one of us is on, we use each others. Since I had forgotten my password because I set it to autologin and therfore did not need to remember it, I just used his. It's a common practice between the two of us. We use each others accounts regularly to traffic the threads and do our business.

Now just because you think you know better than I do does not give you the right to act like I'm some sort of liar. I used to like and respect this site, but I see comments like this all to often. I realize that you are simply trying to keep the legitimate subscribers to your site happy, but going about undermining them and implying that they are lying to you is not the way to go about doing it. Between you, me and my friend, I think that the two of us would know better than you would. Now you have succeeded in greatly offending me by accusing me of lying to you when I compiantly gave you a legitimate response. You are in no position to make that accusation without a knowledge of the truth, which I gave you and you resfused. Now, when confronted, I gave you the correct and appropriate answer, and you decided that, instead of letting it go as your mistake and everybodys happy and no hard feelings, you had the NERVE of TELLING me that I'm lying. I do see that the current position looks somewhat incriminating, but I did tell you exactly what you needed to hear. I had hoped you would have dropped the issue, but apparently you felt the need of pressing it to the point where somebody gets offended.

>> If I have misunderstood this situation or misinterpreted the response here, please let me know.

So I let you know. Then you tell me I'm lying. That makes me feel all warm and fuzzy inside. Wouldn't it you?

>> I would strongly suggest which account you wish to keep.

Then your tone here is just a bit forceful, which I do not appreciate at all. I did cooperate and in this current post I have given you the full explaination, now I will formally request an apology for your mistake(s). I understand YOUR situation and now you understand mine. I hope I can trust that neither I nor he will receive any further harassment on this issue, that this conflict will absolve peacefully and you will continue to have both of us as content, rule abiding users pleased with the results returned from this website.

Cyprus
I apologize for the misunderstanding here.  Moderators do not have the access level needed to validate Email information and other secure profile details, and needed to escalate this to get further detail.  

This is not intended in any way to imply wrongdoing, nor to offend anyone.  The goal was to ensure only one member account exists.  We are all running against the wind, in terms of workload and volumes to be handled, and this one ran awry.  ComTech was in the midst of another issue when I escalated this and hadn't had the time to research the situation further at the initial posting.  I hope that you will both continue participation here and enjoy the excellent team we comprise.  

Regarding the information on the technical side provided by DrDelphi, did this help you achieve your goal here?

Thank you and again, apologies for the misunderstanding.

Moondancer - EE Moderator






Cyprus

Please don't blame Moondancer and ComTech for that. I started the whole thing. Let me explain my situation a bit:

I am trying to clean up the C++ araea. That means I have to go through all the open questions. I have to take a real close look, because I try to be fair to both the asker and the experst when I suggest am answer to a question. That also means I try to follow the logic of all comments and once in a while when I get the SAME thoughts (and even same comments) from Different persons, I post a question in Community Support to check for these logins. It turns out that some people try to beat the system by opening several accounts.

As you have stated now, you are different persons and you used your co-workers account, none of the moderators will object to that. The experts in EE will always try to help you with your problems (and trust me, in this EE TA we've got some pretty good ones!).

I deeply apologize for the trouble I caused. I just hope this will not be a reason to leave EE, since we all try to SHARE oour knowledge. This is a great community and I would be a severe damage for this community if you would leave us being angry. Look at it from our side, didn't it look suspicious after these explanations?

Again, please accept my appologies, I think you made completely clear that you are two differnt persons.

Fair deal?

======
Werner
I wholeheartedly accept your apology and commend you for apologizing at all. I have been to sites where the staff or moderators have somehow thought of themselves as better than all of the rest and what they say goes. This is incredibly frusterating. And I was pleased when I finally found a site that I THOUGHT didn't do that. Your original postings here almost gave me reason to believe that this site was just another where the moderators are "god" so to speak. But it is not. I do realize how incriminating the situation looked and apologize for the time wasted on my behalf. I will happily and willingly continue to use your site and wish you the best of luck.

On the issue of the question itself, it DID help me somewhat, I guess I could give out he points, but I am not wholly satisfied with the result. But since DrDelphi was the only person to even attempt to help me, he deserves some credit.

Cyprus
Thanks, Cyprus.  

A couple of options come to mind here.

1) Reduce the points here for the benefit you feel you got, refunding the rest and then awarding
2) Wait a bit longer to see if DrDelphi has additional insights here
3) Award as is
4) Request a refund and try again to gain current attention, although, as you said .. and I believe the case as well, DrDelphi sure did put a lot into this to help.

Listening further,
Moondancer - EE Moderator