我想在richtextbox中为每个相同的单词着色。我可以为一行但不在多行上。防爆。欢迎“用户”.....
我希望用户这个词在它所在的每一行都是一个精确的颜色。 这是我到目前为止所得到的:
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
,但它只是为最后一个“用户”字着色,而其他字是黑色。
提前谢谢。
答案 0 :(得分:0)
使用模块,您可以这样做:
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希望这有助于:)
答案 1 :(得分:0)
这是一个简单的类,它为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(Chr(32))
ListOfWords.AddRange(Patterns)
在可视示例中,Class以这种方式配置:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Patterns As String() = TextBox1.Text.Split(Chr(32))
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
Class WordList
Private TextRendererFlags As TextFormatFlags = TextFormatFlags.Left Or
TextFormatFlags.Top Or TextFormatFlags.NoPadding Or
TextFormatFlags.WordBreak Or TextFormatFlags.TextBoxControl
Private _Control As RichTextBox = Nothing
Private Words As List(Of Word)
Public Sub New(ByVal RefControl As RichTextBox)
Me._Control = RefControl
Me.Words = New List(Of Word)
Me.ProtectSelection = False
End Sub
Public Property ProtectSelection As Boolean
Public Property HighLightColor As Color
Public Property SelectionColor As Color
Public Sub Add(NewWord As String)
Me.Words.Add(New Word() With {.Word = NewWord, .Indexes = GetWordIndexes(NewWord)})
End Sub
Public Sub AddRange(NewWords As String())
For Each WordItem As String In NewWords
Me.Words.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(Me._Control.Text, Word).
Cast(Of Match)().
Select(Function(chr) chr.Index).ToList()
End Function
Public Sub DeselectAll()
If Me._Control IsNot Nothing Then
Me._Control.SelectAll()
Me._Control.SelectionBackColor = Me._Control.BackColor
Me._Control.Update()
End If
End Sub
Public Sub WordsHighLight()
If Me.Words.Count > 0 Then
For Each WordItem As Word In Me.Words
For Each Position As Integer In WordItem.Indexes
Dim _P As Point = Me._Control.GetPositionFromCharIndex(Position)
TextRenderer.DrawText(Me._Control.CreateGraphics(), WordItem.Word,
Me._Control.Font, _P, Me._Control.ForeColor,
Me.HighLightColor, TextRendererFlags)
Next
Next
End If
End Sub
Public Sub WordsSelect()
Me.DeselectAll()
If Me.Words.Count > 0 Then
For Each WordItem As Word In Me.Words
For Each Position As Integer In WordItem.Indexes
Me._Control.Select(Position, WordItem.Word.Length)
Me._Control.SelectionColor = Me._Control.ForeColor
Me._Control.SelectionBackColor = Me.SelectionColor
Me._Control.SelectionProtected = Me.ProtectSelection
Next
Next
End If
End Sub
Class Word
Property Word As String
Property Indexes As List(Of Integer)
End Class
End Class