I was wondering how I should go about making a TextBox, perhaps RichTextBox, to show Keywords in a seperate color. I've been able to change the text-color of ALL the words in the box...but that doesn't help, I only need certain words, chosen by me, to be colored.
I'm looking for something kind of like Visual Studio, where keywords are changed in color. Like so:
return 0; is what we type
return 0; is what it is changed into
If anyone could help me out, point me in the right direction, anything, it would be GREATLY appreciated!
Thank you in advance,
CSharpHurts
Loading
AlanPosted Mar 4, 2008, 2:13 PM
You'll have to use a RichTextBox for this as I think I'm right in saying that the ordinary TextBox can only support one color at a time but, subject to that, this code should get you on the right track:
richTextBox1.Text =
"The quick brown fox jumped over the lazy dog"; string keyword = "brown"; int index = richTextBox1.Find(keyword);richTextBox1.SelectionStart = index;
richTextBox1.SelectionLength = keyword.Length;
richTextBox1.SelectionColor =
Color.Brown;richTextBox1.SelectionLength = 0;
richTextBox1.SelectionStart = richTextBox1.Text.Length;