-
Nov 4th, 2018, 08:34 AM
#1
Thread Starter
Hyperactive Member
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.
-
Nov 4th, 2018, 08:53 AM
#2
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).
-
Nov 4th, 2018, 08:53 AM
#3
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:
RTB1.Select(2, 4) RTB1.SelectionColor = Color.Blue RTB1.SelectionStart = RTB1.TextLength RTB1.SelectionColor = Color.Black
-
Nov 4th, 2018, 10:41 AM
#4
Thread Starter
Hyperactive Member
Re: Newbie question about RichTextBox font color change
Originally Posted by jmcilhinney
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:
RTB1.Select(2, 4)
RTB1.SelectionColor = Color.Blue
RTB1.SelectionStart = RTB1.TextLength
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.
-
Nov 4th, 2018, 10:56 AM
#5
Thread Starter
Hyperactive Member
Re: Newbie question about RichTextBox font color change
Originally Posted by si_the_geek
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:
If I start writting just when the number 6 ends, I get:
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|