我制作了一个在线文本程序,我想做类似的事情:
If Richtextbox1.text.contains("Tank") Then
Tank.ForeColor = ForeColor.red
End If
我只想要“坦克”这个词是红色的。
答案 0 :(得分:0)
请尝试以下示例。
Public Sub ColorText(box As RichTextBox, color As Color)
box.[Select](start, 5)
box.SelectionColor = color
End Sub
答案 1 :(得分:0)
更新的答案:
我使用RegEx编写了一个解决方案,其优点是能够使用正则表达式模式来查找单词,具有CaseInsensitive,Forecolor,Backcolor,Font等优良功能,并找到多个单词。
用法示例1:
ColorizeWord(RichTextBox1, "Tank", True,
Color.Red, Color.Black,
New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic))
用法示例2:
ColorizeWords(RichTextBox1, {"Tank", "[0-9]"},
IgnoreCase:=False,
ForeColor:=Color.Red, BackColor:=Nothing, Font:=Nothing)
PS:如果您不想设置参数,请将其设置为Nothing
这是:
''' <summary>
''' Find a word on a RichTextBox and colorizes each match.
''' </summary>
''' <param name="RichTextBox">Indicates the RichTextBox.</param>
''' <param name="Word">Indicates the word to colorize.</param>
''' <param name="IgnoreCase">Indicates the ignore case.</param>
''' <param name="ForeColor">Indicates the text color.</param>
''' <param name="BackColor">Indicates the background color.</param>
''' <param name="Font">Indicates the text font.</param>
''' <returns><c>true</c> if matched at least one word, <c>false</c> otherwise.</returns>
Private Function ColorizeWord(ByVal [RichTextBox] As RichTextBox,
ByVal Word As String,
Optional ByVal IgnoreCase As Boolean = False,
Optional ByVal ForeColor As Color = Nothing,
Optional ByVal BackColor As Color = Nothing,
Optional ByVal [Font] As Font = Nothing) As Boolean
' Find all the word matches.
Dim Matches As System.Text.RegularExpressions.MatchCollection =
System.Text.RegularExpressions.Regex.Matches([RichTextBox].Text, Word,
If(IgnoreCase,
System.Text.RegularExpressions.RegexOptions.IgnoreCase,
System.Text.RegularExpressions.RegexOptions.None))
' If no matches then return.
If Not Matches.Count <> 0 Then
Return False
End If
' Set the passed Parameter values.
If ForeColor.Equals(Nothing) Then ForeColor = [RichTextBox].ForeColor
If BackColor.Equals(Nothing) Then BackColor = [RichTextBox].BackColor
If [Font] Is Nothing Then [Font] = [RichTextBox].Font
' Store the current caret position to restore it at the end.
Dim CaretPosition As Integer = [RichTextBox].SelectionStart
' Suspend the control layout to work quicklly.
[RichTextBox].SuspendLayout()
' Colorize each match.
For Each Match As System.Text.RegularExpressions.Match In Matches
[RichTextBox].Select(Match.Index, Match.Length)
[RichTextBox].SelectionColor = ForeColor
[RichTextBox].SelectionBackColor = BackColor
[RichTextBox].SelectionFont = [Font]
Next Match
' Restore the caret position.
[RichTextBox].Select(CaretPosition, 0)
' Restore the control layout.
[RichTextBox].ResumeLayout()
' Return successfully
Return True
End Function
''' <summary>
''' Find multiple words on a RichTextBox and colorizes each match.
''' </summary>
''' <param name="RichTextBox">Indicates the RichTextBox.</param>
''' <param name="Words">Indicates the words to colorize.</param>
''' <param name="IgnoreCase">Indicates the ignore case.</param>
''' <param name="ForeColor">Indicates the text color.</param>
''' <param name="BackColor">Indicates the background color.</param>
''' <param name="Font">Indicates the text font.</param>
''' <returns><c>true</c> if matched at least one word, <c>false</c> otherwise.</returns>
Private Function ColorizeWords(ByVal [RichTextBox] As RichTextBox,
ByVal Words As String(),
Optional ByVal IgnoreCase As Boolean = False,
Optional ByVal ForeColor As Color = Nothing,
Optional ByVal BackColor As Color = Nothing,
Optional ByVal [Font] As Font = Nothing) As Boolean
Dim Success As Boolean = False
For Each Word As String In Words
Success += ColorizeWord([RichTextBox], Word, IgnoreCase, ForeColor, BackColor, [Font])
Next Word
Return Success
End Function
答案 2 :(得分:0)
这是一个简短的快速功能,可以为任何颜色和颜色着色。文字发现的粗体或斜体......
Private Function FormatText(ByVal TextToFormat As String, ByVal TextFormat As Integer, ByVal TextColor As Color)
Dim count As New List(Of Integer)()
For i As Integer = 0 To rText.Text.Length - 1
If rText.Text.IndexOf(TextToFormat, i) <> -1 Then
'If the word is found add the index to the list
count.Add(rText.Text.IndexOf(TextToFormat, i))
End If
Next
Try
For i As Integer = 0 To count.Count - 1
rText.[Select](count(i), TextToFormat.Length)
Select Case TextFormat
Case 1
rText.SelectionFont = New Font(rText.Font.FontFamily, rText.Font.Size, FontStyle.Bold)
rText.SelectionColor = TextColor
Case 2
rText.SelectionFont = New Font(rText.Font.FontFamily, rText.Font.Size, FontStyle.Italic)
rText.SelectionColor = TextColor
End Select
count.RemoveAt(i)
Next
Catch
End Try
Return Nothing
End Function
要使用该功能,下面是一个例子:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If rText.Text.Contains("Tank") Then
FormatText("Tank", 1, Color.Red)
End If
End Sub
以下是我输出的屏幕截图,供您查看...