Colour HTML Tags (2)

This is another example of how to colour HTML tags. Add a RichTextBox to your form, and a command button called cmdColour. Then use the following code


Private Sub ColorHTML(RTF As RichTextBox)
  
   Dim sHTML      As String
   Dim nTagOpen   As Long
   Dim nTagClose  As Long
   Dim nI         As Long
  
   Dim nColor     As Long
  
   sHTML = RTF.Text
   nTagClose = 1
  
   With RTF
      For nI = 1 To Len(sHTML)
         ' *** See where the next tag starts.
         nTagOpen = InStr(nTagClose, sHTML, "<")
         If nTagOpen = 0 Then Exit For
        
         If LCase(Mid(sHTML, nTagOpen, 4)) = "<img" Then
            nColor = &H400040
         ElseIf LCase(Mid(sHTML, nTagOpen, 2)) = "<a" Then
            nColor = &H8080&
         ElseIf LCase(Mid(sHTML, nTagOpen, 2)) = "<b" Then
            nColor = &H8000000F
         Else
            nColor = &H4000&
         End If
        
         ' *** See where the tag ends.
         nTagClose = InStr(nTagOpen, sHTML, ">")
         If nTagOpen = 0 Then nTagClose = Len(sHTML)
         If nTagClose = 0 Then nTagClose = Len(sHTML)
         ' *** Color the tag.
         .SelStart = nTagOpen - 1
         .SelLength = nTagClose - nTagOpen + 1
         .SelColor = nColor
      Next
   End With
  
End Sub

Private Sub cmdColour_Click()
    ColorHTML RichTextBox1
End Sub

You might also like...

Comments

VB Diamond

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson