VB - Fast HTML-Tag coloring (RichTextBox)
Well.. why not post something in here ^^". This snippet is not for serious usage because it resets the whole text formatting when applied to your RTB. For more information see released notes below
VB Code:
Public Function FormatHTML(iTextBox As RichTextBox) As String
On Error Resume Next
Dim PosStart As Long
Dim PosEnd As Long
Dim Temp As String
Dim RTF As String
Dim TempSel as Long
'Setup
Temp = iTextBox.Text
TempSel = iTextBox.SelStart
Temp = Replace(Temp, vbNewLine, "\par ")
Temp = Replace(Temp, vbTab, "\tab ")
'Get first token
PosStart = InStr(PosStart + 1, Temp, "<")
While PosStart > 0
'Add passed text
RTF = RTF & Mid(Temp, PosEnd + 1, PosStart - PosEnd - 1)
'Get end token
PosEnd = InStr(PosStart + 1, Temp, ">")
'Add tag
RTF = RTF & "\cf2 " & Mid(Temp, PosStart, PosEnd - PosStart + 1) & "\cf0 "
'Get next token
PosStart = InStr(PosStart + 1, Temp, "<")
Wend
'Add rest
RTF = RTF & Mid(Temp, PosEnd + 1, Len(Temp))
'Add RTF information
Temp = "{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}" & vbNewLine
Temp = Temp & "{\colortbl ;" & "\red0\green0\blue0;\red128\green0\blue255;" & "}" & vbNewLine
Temp = Temp & "\viewkind4\uc1\pard\cf1\lang2055\f0\fs17 \cf1 " & RTF & "\cf0 }"
'Return
iTextBox.TextRTF = Temp
iTextBox.SelStart = TempSel
End Function
Released notes
The function just marks HTML tags in the color specified in the color table at the end of the code ("\red128\green0\blue255;"). The code does NOT update colorized tags when typing. Also the code does NOT differ between start/end tags in any way nor does it color the text between tags!
Usage
A little addition
In case you want to colorize the < and > indicators you can insert this code:
VB Code:
'Color table (new)
ColorTable = "\red0\green0\blue0;\red128\green0\blue128;\red192\green0\blue0;"
'Add tag (new)
RTF = RTF & "\cf3 " & Mid(Temp, PosStart, 1) & "\cf0 \cf2 " & Mid(Temp, PosStart + 1, PosEnd - PosStart - 1) & "\cf0 \cf3 " & Mid(Temp, PosEnd, 1) & "\cf0 "