Are there libraries that would provide for formatting the content of a text box (or richtextbox) based on the content, assuming the content conforms to a pattern (in essence syntax highlighting)? It would be nice if this were possible in both the web world as well as winform, though I'd prefer winform (or WPF for that matter).
-
Possible duplicate: http://stackoverflow.com/questions/1087735/a-textbox-richtextbox-that-has-syntax-highlighting-c – Fredrik Mörk Jul 22 '09 at 20:11
3 Answers
All that you need to do is to programmatically select text and then set the SelectionColor property. Of course, you will need to write the regular expression(s) that figures out what text to select, but coloring it afterward is simple.
Oh yeah; this will not work for a TextBox, only a RichTextBox (obviously).

- 122,712
- 22
- 185
- 265
You can do this in a rich text box yourself, by just selecting the text and setting the color.
However, there are more elaborate libraries out there...
For example, since you mentioned WinForms, you might want to look at SyntaxEditor by ActiPro.

- 554,122
- 78
- 1,158
- 1,373
This is a little of what you need. It will select the first through the 10th character or to the full length of the RichTextBox then change the color of the selection. The key is that once you make the selection you are making changes on the selection not the whole RichTextBox. Then you can change the font to bold. Bold is a little more wierd.
'select the first character
rtbRichTextBox.SelectionStart = 0
'Select the length forward as far as you need to
rtbRichTextBox.SelectionLength = 10 'Len(rtbRichTextBox.Text)
' change the text color
rtbRichTextBox.SelectionColor = Color.Blue
' make a highlight color over the text
'rtbRichTextBox.SelectionBackColor = Color.Yellow
Dim newFontStyle As System.Drawing.FontStyle
If rtbRichTextBox.SelectionFont IsNot Nothing Then
newFontStyle = FontStyle.Bold
rtbRichTextBox.SelectionFont = New Font(MyObj_Font_Arial.FontFamily, _
MyObj_Font_Arial.Size, _
newFontStyle)
end if
'a more straight forward bold would be to change the font.
Dim MyObjectArialFont As New Font("Arial", 6.5, FontStyle.Bold)
rtbRichTextBox.SelectionFont = MyObjectArialFont