VB - HTML Tag Coloring(Both Realtime and non realtime)
I was playing around with my own lil' notepad editor, and I needed some syntax coloring, so I made this little baby:
1. Put a RichTextBox control on your form, call it "RTF1"
2. Add a module to your project, and insert the following function:
VB Code:
Public Function ColorHTML(RTF As RichTextBox)
'This is NOT realtime coloring - Run after file load etc.
Dim iTagEnd, iTagStart, iTagLength As Integer
Dim sTag As String
'We Need it to go fast, so **** visibility
RTF.Visible = False
For i = 1 To Len(RTF.Text)
If Not Mid(RTF.Text, i, 2) = "<%" Then 'ASP Tag, ignore it
If Mid(RTF.Text, i, 1) = "<" Then
If InStr(i, RTF.Text, ">") > 0 Then
iTagStart = InStr(i, RTF.Text, "<")
iTagEnd = InStr(i, RTF.Text, ">")
iTagLength = iTagEnd - iTagStart
RTF.SelStart = iTagStart
RTF.SelLength = iTagLength - 1
sTag = RTF.SelText
RTF.SelText = ""
RTF.SelColor = &H800000
RTF.SelText = sTag
RTF.SelColor = vbBlack
End If
End If
End If
Next
'We're not coloring anymore - so it's cool if we can see it ;)
RTF.Visible = True
RTF.SelColor = vbBlack
End Function
3. Now make a command button on your form, and in it's Click event put:
4. Run your program and click the command button, everyting inside "< >" will be colored dark blue.
I know this isn't the fastest function - you can almost certainly optimize it, but it works, and it's a good place to start from.
Cheers!