Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

vb.net - Color a specific word in every line of text in a RichTextBox

I want to color every same word inside a RichTextBox. I can do it for one line but not on multiple lines. E.g., Welcome "user" .....

I want the word user to be an exact color in every line it's found.
Here's with what i came up so far:

RichTextBox1.Text = "Welcome "
RichTextBox1.Select(RichTextBox1.TextLength, 0)
RichTextBox1.SelectionColor = My.Settings.color
RichTextBox1.AppendText(My.Settings.username)
RichTextBox1.SelectionColor = Color.Black
RichTextBox1.AppendText(" ........." + vbCrLf)    

It's on form.Load; I tried to use the richtextbox.TextChange event, but it just colors the last user word and the others are remain the same.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a simple Class that enables multiple Selections and Highlights of text for RichTextBox and TextBox controls.
You can use multiple instances of this Class for different controls.

You can add the Words to Select/HighLight to a List and specify which color to use for selecting and/or highlighting the text.

Dim listOfWords As WordList = New WordList(RichTextBox1)

listOfWords.AddRange({"Word1", "Word2"})
listOfWords.SelectionColor = Color.LightBlue
listOfWords.HighLightColor = Color.Yellow

These are the visual results of the Class actions:

RichTextBox Text Select and HighLight

In the example, the List of words is filled using:

Dim patterns As String() = TextBox1.Text.Split()
listOfWords.AddRange(patterns)

In the visual example, the Class is configured this way:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim patterns As String() = TextBox1.Text.Split()

    Dim listOfWords As WordList = New WordList(RichTextBox1)
    listOfWords.AddRange(patterns)
    listOfWords.SelectionColor = Color.LightBlue
    listOfWords.HighLightColor = Color.Yellow

    If RadioButton1.Checked = True Then
        listOfWords.WordsSelect()
    ElseIf RadioButton2.Checked Then
        listOfWords.WordsHighLight()
    Else
        listOfWords.DeselectAll()
    End If
End Sub

This is the Class used to generate the Selections and HighLights:

Imports System.Drawing.Text
Imports System.Text.RegularExpressions

Public Class WordList
    Private TextRendererFlags As TextFormatFlags =
        TextFormatFlags.Top Or TextFormatFlags.Left Or TextFormatFlags.NoPadding Or
        TextFormatFlags.WordBreak Or TextFormatFlags.TextBoxControl

    Private textControl As RichTextBox = Nothing
    Private wordsList As List(Of Word)

    Public Sub New(rtb As RichTextBox)
        textControl = rtb
        wordsList = New List(Of Word)
        ProtectSelection = False
    End Sub

    Public Property ProtectSelection As Boolean
    Public Property HighLightColor As Color
    Public Property SelectionColor As Color

    Public Sub Add(word As String)
        wordsList.Add(New Word() With {.Word = word, .Indexes = GetWordIndexes(word)})
    End Sub

    Public Sub AddRange(words As String())
        For Each WordItem As String In words
            wordsList.Add(New Word() With {.Word = WordItem, .Indexes = GetWordIndexes(WordItem)})
        Next
    End Sub
    Private Function GetWordIndexes(word As String) As List(Of Integer)
        Return Regex.Matches(textControl.Text, word).
                     OfType(Of Match)().
                     Select(Function(chr) chr.Index).ToList()
    End Function

    Public Sub DeselectAll()
        If textControl IsNot Nothing Then
            textControl.SelectAll()
            textControl.SelectionBackColor = textControl.BackColor
            textControl.Update()
        End If
    End Sub

    Public Sub WordsHighLight()
        If wordsList.Count > 0 Then
            For Each WordItem As Word In wordsList
                For Each Position As Integer In WordItem.Indexes
                    Dim p As Point = textControl.GetPositionFromCharIndex(Position)
                    TextRenderer.DrawText(textControl.CreateGraphics(), WordItem.Word,
                                          textControl.Font, p, textControl.ForeColor,
                                          HighLightColor, TextRendererFlags)
                Next
            Next
        End If
    End Sub

    Public Sub WordsSelect()
        DeselectAll()
        If wordsList.Count > 0 Then
            For Each WordItem As Word In wordsList
                For Each Position As Integer In WordItem.Indexes
                    textControl.Select(Position, WordItem.Word.Length)
                    textControl.SelectionColor = textControl.ForeColor
                    textControl.SelectionBackColor = SelectionColor
                    textControl.SelectionProtected = ProtectSelection
                Next
            Next
        End If
    End Sub

    Friend Class Word
        Property Word As String
        Property Indexes As List(Of Integer)
    End Class
End Class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...