Results 1 to 1 of 1

Thread: True Highlighting for RichTextBox

Threaded View

  1. #1

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    True Highlighting for RichTextBox

    The RichTextBox control supports the Rich Text Format codes for highlighting text, but the control itself gives you no access to this functionality.
    Here is a simple routine that adds highlighting
    VB Code:
    1. Public Sub HighLight(RTB As RichTextBox, lColor As Long)
    2. 'add new color to color table
    3. 'add tags \highlight# and \highlight0
    4. 'where # is new color number
    5. Dim iPos As Long
    6. Dim strRTF As String
    7. Dim bkColor As Integer
    8.  
    9.     With RTB
    10.         iPos = .SelStart
    11.         'bracket selection
    12.         .SelText = Chr(128) & .SelText & Chr(129) ' \'80 \'81
    13.         strRTF = RTB.TextRTF
    14. 'add new color
    15.         bkColor = AddColorToTable(strRTF, lColor)
    16. 'add highlighting
    17.          strRTF = Replace(strRTF, "\'80", "\highlight" & CStr(bkColor) & " ")
    18.          strRTF = Replace(strRTF, "\'81", "\highlight0 ")
    19.  
    20.          .TextRTF = strRTF
    21.         .SelStart = iPos
    22.        End With
    23.  
    24. End Sub
    We also need to add the highlight color to the rtf color table if it is not already there
    VB Code:
    1. Function AddColorToTable(strRTF As String, lColor As Long) As Integer
    2. Dim iPos As Long, jPos As Long
    3.  
    4. Dim ctbl As String
    5. Dim tagColors
    6. Dim nColors As Integer
    7. Dim tagNew As String
    8. Dim i As Integer
    9. Dim iLen As Integer
    10. Dim split1 As String
    11. Dim split2 As String
    12.  
    13.     'make new color into tag
    14.     tagNew = "\red" & CStr(lColor And &HFF) & _
    15.         "\green" & CStr(Int(lColor / &H100) And &HFF) & _
    16.         "\blue" & CStr(Int(lColor / &H10000))
    17.    
    18.     'find colortable
    19.     iPos = InStr(strRTF, "{\colortbl")
    20.    
    21.     If iPos > 0 Then 'if table already exists
    22.         jPos = InStr(iPos, strRTF, ";}")
    23.         'color table
    24.         ctbl = Mid(strRTF, iPos + 12, jPos - iPos - 12)
    25.         'array of color tags
    26.         tagColors = Split(ctbl, ";")
    27.         nColors = UBound(tagColors) + 2
    28.         'see if our color already exists in table
    29.         For i = 0 To UBound(tagColors)
    30.             If tagColors(i) = tagNew Then
    31.                 AddColorToTable = i + 1
    32.                 Exit Function
    33.             End If
    34.         Next i
    35. '{\fonttbl{\f0\fnil\fcharset0 Verdana;}}
    36. '{\colortbl ;\red0\green0\blue0;\red128\green0\blue255;}
    37.        
    38.         split1 = Left(strRTF, jPos)
    39.         split2 = Mid(strRTF, jPos + 1)
    40.         strRTF = split1 & tagNew & ";" & split2
    41.         AddColorToTable = nColors
    42.    
    43.     Else
    44.         'color table doesn't exists, let's make one
    45.         iPos = InStr(strRTF, "{\fonttbl") 'beginning of font table
    46.         jPos = InStr(iPos, strRTF, ";}}") + 2 'end of font table
    47.         split1 = Left(strRTF, jPos)
    48.         split2 = Mid(strRTF, jPos + 1)
    49.         strRTF = split1 & "{\colortbl ;" & tagNew & ";}" & split2
    50.         AddColorToTable = 1
    51.     End If
    52.  
    53. End Function
    Last edited by moeur; Aug 17th, 2005 at 09:10 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