views:

566

answers:

3

I can find a million examples of doing reg ex to apply syntax highlighting to a rich text box. but what i need it just a simple way to add in a word of a diffrent color.

What would the code be to just put the words "Hello World" into a textbox and have Hello be red and World be green?

This code doesnt work.

this.richTextBox1.SelectionColor = Color.Red
this.richTextBox1.text += "Test"
+1  A: 

Select the text after you put it in and then change the color.

For example:

richTextBox1.Text += "Test"
richTextBox1.Select(richTextBox1.TextLength - 4, 4)
richTextBox1.SelectionColor = Color.Red
SLaks
A: 

Ive worked with it in VB6 and i think its the same: You must select the text and then apply

this.richTextBox1.SelectionColor = Color.Red

The added text always appears in the defaut color, you must select it and then change its color:

this.richTextBox1.text="Hello world!"
this.richTextBox1.selstart=0
this.richTextBox1.sellength=5
this.richTextBox1.SelectionColor = Color.Red

As i dont use vb.net, you must check the spelling but i think thats the key. The code i wrote is supposed to print "Hello" in red and "World!" in black.

ChaosCoder
A: 

This code adds text "Hello" in red color and "World" in green to the RichTextBox (tested in VS2008).

RichTextBox1.SelectionColor = Color.Red
RichTextBox1.SelectedText = "Hello "
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = "World"
Meta-Knight
Why was I downvoted... I tested my code and it works.
Meta-Knight