Different colored text - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Different colored text

I need to show text in paragraph format in a window within my main program. I thought of using a multiline edit control, but I also need to be able to change the color of some of the words within the paragraph. The user will not be editing the text, just scrolling through and reading it. What is the best way to accomplish this?

Comments

  • zibadianzibadian Posts: 6,349Member
    : I need to show text in paragraph format in a window within my main program. I thought of using a multiline edit control, but I also need to be able to change the color of some of the words within the paragraph. The user will not be editing the text, just scrolling through and reading it. What is the best way to accomplish this?
    :
    I would use the TRichEdit. Write the text using your favorite word processor (such as word) and save it as an .rtf file. This you can load into the TRichEdit during design-time or runtime (as needed or wished). Make sure to set the ReadOnly property to true.
  • StatManLVStatManLV Posts: 7Member
    : : I need to show text in paragraph format in a window within my main program. I thought of using a multiline edit control, but I also need to be able to change the color of some of the words within the paragraph. The user will not be editing the text, just scrolling through and reading it. What is the best way to accomplish this?
    : :
    : I would use the TRichEdit. Write the text using your favorite word processor (such as word) and save it as an .rtf file. This you can load into the TRichEdit during design-time or runtime (as needed or wished). Make sure to set the ReadOnly property to true.
    :
    I'm not familiar with TRichEdit. Also, the text that will be displayed is dynamic. The program is for computer interviewing and so the text that is shown can and does change quite often.
  • jamesb800jamesb800 Posts: 105Member
    : : : I need to show text in paragraph format in a window within my main program. I thought of using a multiline edit control, but I also need to be able to change the color of some of the words within the paragraph. The user will not be editing the text, just scrolling through and reading it. What is the best way to accomplish this?
    : : :
    : : I would use the TRichEdit. Write the text using your favorite word processor (such as word) and save it as an .rtf file. This you can load into the TRichEdit during design-time or runtime (as needed or wished). Make sure to set the ReadOnly property to true.
    : :
    : I'm not familiar with TRichEdit. Also, the text that will be displayed is dynamic. The program is for computer interviewing and so the text that is shown can and does change quite often.


    You might try this...
    Place a TRichEdit on the form and insert this
    [code]
    procedure ChangeText(re:TRichEdit; Word :string; fName:string;
    aColor:TColor; aSize:integer; fIndent:integer;aStyle: TFontStyles);
    .....

    procedure TForm1.ChangeText(re:TRichEdit; Word :string;
    fName:string; aColor:TColor; aSize:integer; fIndent:integer;
    aStyle: TFontStyles);
    var
    cp, cp2: Integer;
    begin
    cp := 0;
    with re do begin
    repeat
    cp2 := FindText(Word, cp,length(lines.Text), [stWholeWord]);
    cp := cp2+1;
    SelStart := cp2;
    SelLength:=Length(Word);
    SelAttributes.Color:=aColor;
    SelAttributes.Name:=fName;
    SelAttributes.Style:=aStyle;
    SelAttributes.Size:=aSize;
    Paragraph.FirstIndent := fIndent;
    until
    cp = 0;
    end;
    end;
    [/code]

    Call it like this:
    [code]
    procedure TForm1.Button11Click(Sender: TObject);
    begin
    ChangeText(RichEdit1,'Test','Arial',clblue,16,0,[fsBold]);
    end;
    [/code]

    You may want to get rid of the indent, size and fontname.
    There are probably better ways to do this, but this works for me.

    James


  • StatManLVStatManLV Posts: 7Member
    : : : : I need to show text in paragraph format in a window within my main program. I thought of using a multiline edit control, but I also need to be able to change the color of some of the words within the paragraph. The user will not be editing the text, just scrolling through and reading it. What is the best way to accomplish this?
    : : : :
    : : : I would use the TRichEdit. Write the text using your favorite word processor (such as word) and save it as an .rtf file. This you can load into the TRichEdit during design-time or runtime (as needed or wished). Make sure to set the ReadOnly property to true.
    : : :
    : : I'm not familiar with TRichEdit. Also, the text that will be displayed is dynamic. The program is for computer interviewing and so the text that is shown can and does change quite often.
    :
    :
    : You might try this...
    : Place a TRichEdit on the form and insert this
    : [code]
    : procedure ChangeText(re:TRichEdit; Word :string; fName:string;
    : aColor:TColor; aSize:integer; fIndent:integer;aStyle: TFontStyles);
    : .....
    :
    : procedure TForm1.ChangeText(re:TRichEdit; Word :string;
    : fName:string; aColor:TColor; aSize:integer; fIndent:integer;
    : aStyle: TFontStyles);
    : var
    : cp, cp2: Integer;
    : begin
    : cp := 0;
    : with re do begin
    : repeat
    : cp2 := FindText(Word, cp,length(lines.Text), [stWholeWord]);
    : cp := cp2+1;
    : SelStart := cp2;
    : SelLength:=Length(Word);
    : SelAttributes.Color:=aColor;
    : SelAttributes.Name:=fName;
    : SelAttributes.Style:=aStyle;
    : SelAttributes.Size:=aSize;
    : Paragraph.FirstIndent := fIndent;
    : until
    : cp = 0;
    : end;
    : end;
    : [/code]
    :
    : Call it like this:
    : [code]
    : procedure TForm1.Button11Click(Sender: TObject);
    : begin
    : ChangeText(RichEdit1,'Test','Arial',clblue,16,0,[fsBold]);
    : end;
    : [/code]
    :
    : You may want to get rid of the indent, size and fontname.
    : There are probably better ways to do this, but this works for me.
    :
    : James
    :
    :
    : I will give that a try. Thanks!!
Sign In or Register to comment.