【问题标题】:Color a specific word in every line of text in a RichTextBox为 RichTextBox 中每一行文本中的特定单词着色
【发布时间】:2018-08-26 09:17:25
【问题描述】:

我想为 RichTextBox 中的每个相同单词着色。我可以在一行上做,但不能在多行上做。 例如,欢迎“用户”.....

我希望 user 这个词在它找到的每一行中都是一个准确的颜色。
到目前为止,这是我想出的:

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)    

form.Load;我尝试使用 richtextbox.TextChange 事件,但它只是为最后一个 user 单词着色,其他单词保持不变。

【问题讨论】:

    标签: vb.net winforms richtextbox highlight


    【解决方案1】:

    有了模块,你可以这样做:

    Imports System.Runtime.CompilerServices
    
    Module Utility
    
    <Extension()>
    Sub HighlightText(ByVal myRtb As RichTextBox, ByVal word As String, ByVal color As Color)
        If word = String.Empty Then Return
        Dim index As Integer, s_start As Integer = myRtb.SelectionStart, startIndex As Integer = 0
        While(__InlineAssignHelper(index, myRtb.Text.IndexOf(word, startIndex))) <> -1
            myRtb.[Select](index, word.Length)
            myRtb.SelectionColor = color
            startIndex = index + word.Length
        End While
    
        myRtb.SelectionStart = s_start
        myRtb.SelectionLength = 0
        myRtb.SelectionColor = Color.Black
    End Sub
    
    <Obsolete("Please refactor code that uses this function, it is a simple work-around to simulate inline assignment in VB!")>
    Private Shared Function __InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
        target = value
        Return value
    End Function
    End Module
    

    或者,您也可以使用这个,因为它可以让您同时突出显示多个单词:

     Private Sub HighlightWords(ByVal words() As String)
         Private Sub HighlightWords(ByVal words() As String)
        For Each word As String In words
            Dim startIndex As Integer = 0
    
            While (startIndex < rtb1.TextLength)
                Dim wordStartIndex As Integer = rtb1.Find(word, startIndex, RichTextBoxFinds.None)
                If (wordStartIndex <> -1) Then
                    rtb1.SelectionStart = wordStartIndex
                    rtb1.SelectionLength = word.Length
                    rtb1.SelectionBackColor = System.Drawing.Color.Black
                Else
                    Exit While
                End If
    
                startIndex += wordStartIndex + word.Length
            End While
    
        Next
    End Sub
    

    Source希望这会有所帮助:)

    【讨论】:

    • 我尝试调整两个代码以匹配我的项目,但这些代码都没有实际帮助。
    • 你是什么意思?它应该可以工作..你能显示你试过的代码吗?
    【解决方案2】:

    这是一个简单的类,可以为 RichTextBox 和 TextBox 控件启用多个文本选择和突出显示。
    您可以将此类的多个实例用于不同的控件。

    您可以将要选择/突出显示的单词添加到列表中,并指定用于选择和/或突出显示文本的颜色。

    Dim listOfWords As WordList = New WordList(RichTextBox1)
    
    listOfWords.AddRange({"Word1", "Word2"})
    listOfWords.SelectionColor = Color.LightBlue
    listOfWords.HighLightColor = Color.Yellow
    

    这些是类操作的视觉结果:

    在示例中,单词列表使用以下内容填充:

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

    在视觉示例中,类是这样配置的:

    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
    

    这是用于生成选择和高光的类:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2016-11-09
      • 1970-01-01
      • 2011-04-15
      相关资源
      最近更新 更多