Results 1 to 1 of 1

Thread: VB - Fast HTML-Tag coloring (RichTextBox)

  1. #1

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    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:
    1. Public Function FormatHTML(iTextBox As RichTextBox) As String
    2. On Error Resume Next
    3.  
    4.     Dim PosStart As Long
    5.     Dim PosEnd As Long
    6.    
    7.     Dim Temp As String
    8.     Dim RTF As String
    9.    
    10.     Dim TempSel as Long
    11.    
    12.     'Setup
    13.     Temp = iTextBox.Text
    14.     TempSel = iTextBox.SelStart
    15.    
    16.     Temp = Replace(Temp, vbNewLine, "\par ")
    17.     Temp = Replace(Temp, vbTab, "\tab ")
    18.    
    19.     'Get first token
    20.     PosStart = InStr(PosStart + 1, Temp, "<")
    21.    
    22.     While PosStart > 0
    23.         'Add passed text
    24.         RTF = RTF & Mid(Temp, PosEnd + 1, PosStart - PosEnd - 1)
    25.        
    26.         'Get end token
    27.         PosEnd = InStr(PosStart + 1, Temp, ">")
    28.        
    29.         'Add tag
    30.         RTF = RTF & "\cf2 " & Mid(Temp, PosStart, PosEnd - PosStart + 1) & "\cf0 "
    31.        
    32.         'Get next token
    33.         PosStart = InStr(PosStart + 1, Temp, "<")
    34.     Wend
    35.    
    36.     'Add rest
    37.     RTF = RTF & Mid(Temp, PosEnd + 1, Len(Temp))
    38.    
    39.     'Add RTF information
    40.     Temp = "{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}" & vbNewLine
    41.     Temp = Temp & "{\colortbl ;" & "\red0\green0\blue0;\red128\green0\blue255;" & "}" & vbNewLine
    42.     Temp = Temp & "\viewkind4\uc1\pard\cf1\lang2055\f0\fs17 \cf1 " & RTF & "\cf0 }"
    43.    
    44.     'Return
    45.     iTextBox.TextRTF = Temp
    46.     iTextBox.SelStart = TempSel
    47. 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
    VB Code:
    1. FormatHTML myTextBox


    A little addition
    In case you want to colorize the < and > indicators you can insert this code:

    VB Code:
    1. 'Color table (new)
    2.     ColorTable = "\red0\green0\blue0;\red128\green0\blue128;\red192\green0\blue0;"
    3.  
    4.  
    5.         'Add tag (new)
    6.         RTF = RTF & "\cf3 " & Mid(Temp, PosStart, 1) & "\cf0 \cf2 " & Mid(Temp, PosStart + 1, PosEnd - PosStart - 1) & "\cf0 \cf3 " & Mid(Temp, PosEnd, 1) & "\cf0 "
    Last edited by Fox; Apr 21st, 2003 at 01:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width