Results 1 to 2 of 2

Thread: Adding text to rtf box

  1. #1
    Guest

    Question

    How can I add multi-colored text at run-time to a rich text box using code without destroying its existing content? I don’t mean loading a file, but adding strings to it. The rtf box is locked and used for display only.

  2. #2
    Guest

    Lightbulb

    Hello bkspace,

    You will need to parse the text in the richtextbox for the words you want to change the color for.

    Once you find a word you then would use the the selstart, sellen and selcolor proporties to change the color of the word.

    As such:
    Code:
     Private Sub Command1_Click()
      
      Dim I As Long, lRtn As Long
      Dim sSrchText As String
      
      sSrchText = "Word" ' text to look for
      
      For I = 1 To Len(rtb1.Text) ' set up a loop to search the text
        lRtn = InStr(I, rtb1.Text, sSrchText, vbTextCompare) 'search for the text
        If lRtn > 0 Then ' if you found something change the color
          rtb1.SelStart = lRtn - 1
          rtb1.SelLength = Len(sSrchText)
          rtb1.SelColor = QBColor(2)
          I = lRtn ' normally you would not do this but it does speed things up
        Else
          Exit For ' if you don't find anymore exit the loop
        End If
      Next I
      
      
    End Sub
    Hope this helps a little,

    Roger

    [Edited by RvA on 04-16-2000 at 04:26 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