Results 1 to 5 of 5

Thread: Newbie question about RichTextBox font color change

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Newbie question about RichTextBox font color change

    Hi there, I am programming a simple software where I want to change the color of specific words.

    I am using richtextbox control and to change the color of the font, first I have to select the text and after change the color.
    Example:
    Code:
    RTB1.Select(2, 4)
    RTB.SelectionColor = Color.Blue
    I have been reading the documentation on MSDN site, and it explains that SelectionColor will change the color of the selected text and the following text that the user writes.

    The problem is I don't know how to handle this situation because I want to change the color of a specific text only but I don't want that the color of the new words that I write after, are also of Color.Blue (usually the font color is white in all the text).

    Any advices will be welcomed.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Newbie question about RichTextBox font color change

    As long as there is at least one character after the selection (even if it is a space), the colour will not continue.

    Obviously we don't know what you are actually doing in the RichTextBox (eg: we don't know what word(s) you are colouring, or when), but sensible ways to deal with it include:
    • Only colour the word when there is a space/punctuation after it (which makes a lot of sense if the word could be part of a different longer word).
    • Automatically add a space after the word (which could be annoying if the user is typing and not expecting the space).
    • If the selection goes to the end of the text, use variable(s) to remember that, and then when another character is typed go back and set the colour again (after removing the colour from that text and the new character).

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Newbie question about RichTextBox font color change

    It will only change the colour of subsequent text if you do it at the end of the current text. If you change text somewhere else then you can type at the end of the text and it won't be affected. If you have set the colour at the end of the text then all you need to do is set the SelectionStart property to the end of the text and then set the SelectionColor to the correct value you want for subsequent text, e.g.
    vb.net Code:
    1. RTB1.Select(2, 4)
    2. RTB1.SelectionColor = Color.Blue
    3. RTB1.SelectionStart = RTB1.TextLength
    4. RTB1.SelectionColor = Color.Black

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: Newbie question about RichTextBox font color change

    Quote Originally Posted by jmcilhinney View Post
    It will only change the colour of subsequent text if you do it at the end of the current text. If you change text somewhere else then you can type at the end of the text and it won't be affected. If you have set the colour at the end of the text then all you need to do is set the SelectionStart property to the end of the text and then set the SelectionColor to the correct value you want for subsequent text, e.g.
    vb.net Code:
    1. RTB1.Select(2, 4)
    2. RTB1.SelectionColor = Color.Blue
    3. RTB1.SelectionStart = RTB1.TextLength
    4. RTB1.SelectionColor = Color.Black
    Thank you very much. That it was the information I was looking for.

    I am going to tell you in detail what I was doing:
    I have the text "123456789" in the first line of the richtextbox. When I execute the code:
    Code:
    RTB.Select(2, 4)
    RTB1.SelectionColor = Color.Blue
    The text "3456" gets blue. That is fine. The "problem" is if I write just after the number 6, the color of the new text is blue also and I want the color to be as the rest of the text (e.g. black). That "problem" doesn't happen if the text is "123456" and I write after the number 6 new text. In this case, using your code, the new text is black as I like (not blue). But, If the original text is longer (e.g. 123456789) and I write just after the number 6, the color of the new text is blue.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: Newbie question about RichTextBox font color change

    Quote Originally Posted by si_the_geek View Post
    As long as there is at least one character after the selection (even if it is a space), the colour will not continue.

    Obviously we don't know what you are actually doing in the RichTextBox (eg: we don't know what word(s) you are colouring, or when), but sensible ways to deal with it include:
    • Only colour the word when there is a space/punctuation after it (which makes a lot of sense if the word could be part of a different longer word).
    • Automatically add a space after the word (which could be annoying if the user is typing and not expecting the space).
    • If the selection goes to the end of the text, use variable(s) to remember that, and then when another character is typed go back and set the colour again (after removing the colour from that text and the new character).

    I describe the situation.
    I have just one single line of text that is "123456789"
    If I execute
    Code:
    RTB1.Select(2, 4)
    RTB1.SelectionColor = Color.Red
    I obtain:
    Name:  vhQXdB.png
Views: 161
Size:  741 Bytes

    If I start writting just when the number 6 ends, I get:
    Name:  agG2gy.png
Views: 239
Size:  1,005 Bytes

    That doesn't happens if the original text is "123456" and I add to the code
    Code:
    RTB1.SelectionStart = RTB1.TextLength
    RTB1.SelectionColor = Color.White
    Name:  q9TuT9.png
Views: 238
Size:  930 Bytes

    I have even tried the following code without any result (on the string "123456789") because new text is also red:
    Code:
    RTB1.SelectionStart = 6
    RTB1.SelectionColor = Color.White

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width