I need to change the color of every line starting with a "#" to green. How can I do this. I have searched around the forums and i have found nothing. Any help would be great thanks.
Printable View
I need to change the color of every line starting with a "#" to green. How can I do this. I have searched around the forums and i have found nothing. Any help would be great thanks.
I can't think that it's been a very thorough search as this sort of thing is a fairly popular question. I saw a post earlier today on essentially the same topic if I'm not mistaken. There's even a thread dedicated to syntax highlighting in a RichTextBox in the VB.NET CodeBank forum. I would also think that reading the MSDN documentation for the RichTextBox would yield the members you needed to use, as they are quite few.
Anyway, you can use IndexOf to find the next instance of the '#' character and set the SelectionStart property. You can then use IndexOf to find the end of the current line and set the SelectionLength property. You can then set the SelectionColor property. Repeat this procedure until you reach the end of the text.
try this:
vb Code:
For x As Integer = 0 To RichTextBox1.Lines.GetUpperBound(0) If RichTextBox1.Lines(x).StartsWith("#") Then RichTextBox1.SelectionStart = RichTextBox1.GetFirstCharIndexFromLine(x) RichTextBox1.SelectionLength = RichTextBox1.Lines(x).Length RichTextBox1.SelectionColor = Color.Green End If Next
with a little modification your code worked great. thanks for the help