C# How can I set the color of text in a richtextbox?

前端 未结 2 886
梦如初夏
梦如初夏 2021-01-02 23:53

I have a richtextbox in an app and I\'d like key words to be in another color, how can I do this?

Thanks Jade

相关标签:
2条回答
  • 2021-01-02 23:59

    you can use:

    richTextBox1.SelectionColor = Color.Yellow;
    richTextBox1.SelectionBackColor = Color.Blue;
    

    to select a text set:

    richTextBox1.SelectionStart = text_position_in_editor (caret position)
    
    richTextBox1.SelectionLength = text_length
    

    then set SelectionColor ... (see all properties starting with Selection...)

    Or, you can set directly the Rtf property (or richTextBox1.SelectedRtf) which contains formatting info.

    Type some text in WordPad, change it's formatting, copy and paste it into your RichTextBox, show the property Rtf value, you should be able to learn faster how to meet your needs.

    0 讨论(0)
  • 2021-01-03 00:20

    You need to encode the text added to the box in a Rich Text Markup which specifies the particular color. Here is a link to the basics of Rich Text marker

    • http://en.wikipedia.org/wiki/Rich_Text_Format
    0 讨论(0)
提交回复
热议问题