Results 1 to 3 of 3

Thread: [RESOLVED] Color Lines of RichTextBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    169

    Resolved [RESOLVED] Color Lines of RichTextBox

    Hi, I have this richtextbox where I'd like to color an entire line of it a certain color whenever I see a certain word.

    For example, if I type in "Hello, how are you today?" Let's say "you" is the word I'm looking for. When it finds this word, I'd like it to color the line blue, but the actual word another color like red.

    Like this. "Hello, how are you today?"

    Anyone have any ideas? I know I need to use SelLength, SelColor and what not, but not sure how.

  2. #2
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Color Lines of RichTextBox

    there are faster ways, but this will do it:
    VB Code:
    1. Private Sub ColorText(ByRef Word As String, ByRef Color As Long)
    2.     Dim lMrkr As Long
    3.     Dim sText As String
    4.    
    5.     sText = RichTextBox1.Text
    6.     lMrkr = InStr(sText, Word)
    7.     Do While lMrkr
    8.         RichTextBox1.SelStart = lMrkr - 1
    9.         RichTextBox1.SelLength = Len(Word)
    10.         RichTextBox1.SelColor = Color
    11.         lMrkr = InStr(lMrkr + 1, sText, Word)
    12.     Loop
    13. End Sub

    Oups, just saw that you want to color the line too.
    You can use the Split function to break the text into lines for testng/coloring, then go back and do the words.
    Last edited by longwolf; Jan 15th, 2007 at 05:30 PM.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Color Lines of RichTextBox

    Here you go

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim lngCount As Long
    3.     Dim lngLineIndex As Long
    4.     Dim lngLength As Long
    5.     Dim lngTotLength As Long
    6.     Dim strBuffer As String
    7.     Dim strRichText As String
    8.     Dim i As Integer
    9.     Dim intPos As Integer
    10.     Const WORD_TO_BE_FOUND = "you"
    11.    
    12.     'Get Line count
    13.     lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
    14.     With RichTextBox1
    15.         For i = 0 To lngCount - 1
    16.             'Get line index
    17.             lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
    18.             'get line length
    19.             lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
    20.             'resize buffer
    21.             strBuffer = Space(lngLength)
    22.             'get line text
    23.             Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
    24.             ' see if the line contains the word
    25.             intPos = InStr(1, strBuffer, WORD_TO_BE_FOUND)
    26.             If intPos > 0 Then
    27.                 ' color the line blue
    28.                 .SelStart = lngTotLength
    29.                 .SelLength = lngLength
    30.                 .SelColor = vbBlue
    31.                 ' color the word red
    32.                 .SelStart = intPos + lngTotLength - 1
    33.                 .SelLength = Len(WORD_TO_BE_FOUND)
    34.                 .SelColor = vbRed
    35.             End If
    36.             ' Accumulate the total characters read
    37.             lngTotLength = lngTotLength + lngLength
    38.         Next
    39.     End With
    40. End Sub

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