Results 1 to 2 of 2

Thread: Anyone know how to color specefic words in a text box?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2000
    Posts
    19

    Post

    I wan't to be able to color things like follows:

    Private Sub Command1_Click()
    Dim I As Integer
    For I = 1 To 90
    TextBox1.BackColor = RGB(Red As Integer, Green As Integer, Blue As Integer)
    Next I
    End Sub

    Except where i chose wich words are wich colors... could someone give me an example of this?

  2. #2
    Guest
    This example will highlight the words you specify, in blue. Make a Form with a RichTextBox on it.
    Put the following code in the Declarations section of your Form.

    Code:
    Option Explicit
    
    Private Sub HighlightText(sKeyword As String)
    
    Dim nStart As Integer, sPrevChar As String, sNextChar As String
        
    nStart = InStr(1, LCase(RichTextBox1.Text), sKeyword)
    
    Do While nStart <> 0
    If nStart > 1 Then
        sPrevChar = Mid$(RichTextBox1.Text, nStart - 1, 1)
    Else
        sPrevChar = " "
    End If
                                                    
    
    If Len(RichTextBox1.Text) >= nStart + Len(sKeyword) Then
        sNextChar = Mid$(RichTextBox1.Text, nStart + Len(sKeyword), 1)
    Else
        sNextChar = " "
    End If
                                             
    
    If (sPrevChar = Chr(32) Or sPrevChar = Chr(13) Or _
    sPrevChar = Chr(10) Or sPrevChar = Chr(9)) And _
    (sNextChar = Chr(32) Or sNextChar = Chr(13) Or _
    sNextChar = Chr(10) Or sNextChar = Chr(9)) Then
    
    With RichTextBox1
    
        .SelStart = nStart - 1
        .SelLength = Len(sKeyword)
        .SelColor = vbBlue
        .SelText = UCase(sKeyword)
        .SelStart = Len(RichTextBox1.Text)
        .SelColor = vbBlack
    
    End With
    End If
    
                                                    
    
    nStart = InStr(nStart + Len(sKeyword), LCase(RichTextBox1.Text), sKeyword)
    
    Loop
    
    End Sub
    
    
    Private Sub Form_Resize()
    
    RichTextBox1.Move 0, 0, ScaleWidth, ScaleHeight
    
    End Sub
    
    Private Sub RichTextBox1_Change()
    
    With RichTextBox1
        .SelStart = 0
        .SelLength = Len(.Text)
        .SelColor = vbBlack
        .SelStart = Len(.Text)
    End With
    
                                                
    ' You can add words to the highlight list. I have game you
    ' a few to start off with.
    HighlightText "<html>"
    HighlightText "<title>"
    HighlightText "<body>"
    HighlightText "</html>"
    HighlightText "</title>"
    HighlightText "</body>"
                                
    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