C# - Form: different words in richtextbox to be different colors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roucha
    New Member
    • Aug 2008
    • 3

    C# - Form: different words in richtextbox to be different colors

    I have created a chat program, and i want the usernames to be a different color then the other words in the richtextbox that i have.
    i have googled this and i did not find any suitable answer.
    Please help.
    -thanks
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    There are a couple of ways to do this. One is to have a bit of know-how with RTF(can be hard).
    The other is (maybe only in .NET2.0 and later) to use the proeprties outlines for the RichTextBox.
    .SelectionColor and .SelectionBackC olor (among others)
    You would set these to the colors you would want, append the username to the text, then change the colors back to the regular values (black text on white background for example)

    As an example:
    [code=c#]
    void AddMessage(stri ng Username, string MsgText)
    {
    myRichTextBox.S electionColor = Color.Red;
    myRichTextBox.A ppendText(Usern ame);
    myRichTextBox.S electionColor = Color.Black;
    myRichTextBox.A ppendText(MsgTe xt);
    }
    [/code]

    Comment

    Working...