Results 1 to 3 of 3

Thread: [RESOLVED] RichText Change Color of all Certain Words Only

  1. #1

    Thread Starter
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Resolved [RESOLVED] RichText Change Color of all Certain Words Only

    I have been searching around and can't find anything that works for this. Basically I just want to be able to change the colors of certain words in a richtextbox

    for example. I want it to look like this in the richtextbox when it gets certain words like the names of the colors etc.
    Normal:Blue:Red:Green:Normal:Normal:Blue:Red:Green:Normal

    any help would be appreciated.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: RichText Change Color of all Certain Words Only

    try,
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            ColorWord(RichTextBox1, "Blue", Color.Blue)
            ColorWord(RichTextBox1, "Red", Color.Red)
        End Sub
    
    
        Public Sub ColorWord(rtb As RichTextBox, word As String, color As Color)
            Dim start, found As Integer
            With rtb
                found = .Find(word, start, RichTextBoxFinds.WholeWord Or RichTextBoxFinds.NoHighlight)
                Do While found > -1
                    .SelectionStart = found
                    .SelectionLength = word.Length
                    .SelectionColor = color
                    start = found + word.Length
                    If start >= .TextLength Then Exit Do
                    found = .Find(word, start, RichTextBoxFinds.WholeWord Or RichTextBoxFinds.NoHighlight)
                Loop
            End With
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: RichText Change Color of all Certain Words Only

    thanks works great!

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