Solved

Moving font styles from one RichEdit to another

Posted on 2002-03-22
19
429 Views
Last Modified: 2013-12-03
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 >> ?
0
Comment
Question by:shiloh_state
  • 6
  • 4
  • 3
  • +3
19 Comments
 
LVL 6

Accepted Solution

by:
DrDelphi earned 300 total points
Comment Utility
This is a Delphi example (Sorry, no BCB here at work), but you should get the idea.

  var loopint:integer;
      SelectedText:Pchar;
      Orig:integer;
      Dest:Integer;
  begin
    Orig:=RichEdit1.SelStart;
    Dest:=RichEdit1.SelLength;
    for loopint:=Orig to Dest do
    begin
      RichEdit1.SelStart:=Loopint;
      RichEdit1.Sellength:=1;
      RichEdit2.SelStart:=Length(RichEdit2.text);
      RichEdit2.SelAttributes:=RichEdit1.SelAttributes;
      RichEdit2.SelText:=RichEdit1.SelText;
    end;
  end;



Good luck!!

0
 

Author Comment

by:shiloh_state
Comment Utility
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.
0
 
LVL 6

Expert Comment

by:DrDelphi
Comment Utility
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!!
0
 
LVL 6

Expert Comment

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


0
 

Author Comment

by:shiloh_state
Comment Utility
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.
0
 
LVL 6

Expert Comment

by:DrDelphi
Comment Utility
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!!
0
 
LVL 6

Expert Comment

by:DrDelphi
Comment Utility
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!!
0
 

Author Comment

by:shiloh_state
Comment Utility
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
0
 
LVL 6

Expert Comment

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


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).

 

Expert Comment

by:cyprus106
Comment Utility
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;
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 1

Expert Comment

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

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

Thank you,
Moondancer - EE Moderator
0
 

Expert Comment

by:cyprus106
Comment Utility
i apologize. One is my friends account. I had him send a reply when I forgot my password.
0
 

Expert Comment

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

Expert Comment

by:cyprus106
Comment Utility
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
0
 
LVL 1

Expert Comment

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






0
 
LVL 11

Expert Comment

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

Expert Comment

by:cyprus106
Comment Utility
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
0
 
LVL 1

Expert Comment

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

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

18 Experts available now in Live!

Get 1:1 Help Now