locked
how to change background color of text inside a richtextbox?

    Question

  • How do you change the background color of certain text inside of a richtextbox, and only affect the background color of this specific text which you know the offset & length of.

    For example:
    You have a richtextbox with the string "Hello" in it
    I would like to put a yellow background around the characters "lo" at then end of the word Hello
    That is at offset 3 to offset 4 to have a yellow background

    I tried something like this:
    RichTextBox1.Text.Substring(0, 5) = System.Drawing.Color.Azure
    But it didn't work..

    Thanks for the help
    Friday, February 01, 2008 5:39 PM

Answers

  • This will work with your static example (meaning a Text value of 'Hello').  You would want to do a search of the text and find the starting index of a match to your search.  Then you set the length and the color....

     

    RichTextBox1.SelectionStart = 3

    RichTextBox1.SelectionLength = 2

    RichTextBox1.SelectionBackColor = Color.Yellow

     

    Friday, February 01, 2008 5:48 PM

All replies

  • This will work with your static example (meaning a Text value of 'Hello').  You would want to do a search of the text and find the starting index of a match to your search.  Then you set the length and the color....

     

    RichTextBox1.SelectionStart = 3

    RichTextBox1.SelectionLength = 2

    RichTextBox1.SelectionBackColor = Color.Yellow

     

    Friday, February 01, 2008 5:48 PM
  • awesome!
    thanks
    Friday, February 01, 2008 6:01 PM