[RESOLVED] [02/03] Change the Textcolor in a RichTextbox.
Hi All,
What I'm trying to do is to change the Forecolor of each letter in a RichtextBox.
I can already change the ForeColor but that's for all of the Text and not for each letter.
This is what I've got:
VB Code:
If ColorDialog1.ShowDialog <> DialogResult.Cancel Then
RichTextBox1.ForeColor = ColorDialog1.Color
End If
Thanks in advance,
sparrow1
Re: [02/03] Change the Textcolor in a RichTextbox.
Hi, you can't do it (to my knowledge) without going through selecting the letters you want to change the color of. This is how I do it:
VB Code:
RichTextBox1.SelectionColor = Color.Red
Re: [02/03] Change the Textcolor in a RichTextbox.
You can loop through the text and work with selection length?
VB Code:
this.richTextBox1.Text = "abcdefg";
this.richTextBox1.SelectionStart = 1;
this.richTextBox1.SelectionLength = 3;
this.richTextBox1.SelectionColor = System.Drawing.Color.Red;
this.richTextBox1.SelectionStart = 4;
this.richTextBox1.SelectionLength = 2;
this.richTextBox1.SelectionColor = System.Drawing.Color.Blue;
this.richTextBox1.SelectionLength = 0;
Re: [02/03] Change the Textcolor in a RichTextbox.
Quote:
Originally Posted by Half
Hi, you can't do it (to my knowledge) without going through selecting the letters you want to change the color of. This is how I do it:
VB Code:
RichTextBox1.SelectionColor = Color.Red
Hi Half,
That solved already my problem!
Wkr,
sparrow1
Re: [RESOLVED] [02/03] Change the Textcolor in a RichTextbox.
The selection thing is really fishy, if I was the user it'd look pretty lame to see some letters getting selected all the time I want to change the color. Also the selection should be unselected again so that the new color can be visible.
If the user clicks anywhere in the richtextbox, then the whole selection logic in code goes to hell. Esp. if you want to color sequences after the first colored sequence. I always put the first selection's end in some var as a reference from where on to continue another coloring.
I have experimented with setting the HideSelection to True, so the user doesn't notice the text to be colored go selected, then again unselected but this reloads the whole text when you reset HideSelection back to False.
All this apply also if you want to underline (or make bold etc) only parts of the text.
Another problem, if the user scrolls down and the text to be selected is above the visible text AND ends exactly at the end of a line, changing the color will scroll to the end of the selection thus not showing no colored text but the line after that. In this case you must move the caret to the start of the selection and then use the scrolltocaret method. Yet another variable to hold the selection start...
Everyone would notice those things, I just thought to share my experience to make your work a bit easier :)