2014-02-19 39 views
1

我做了一个在线文字计划,我想要做的事,如:颜色的某些文本

If Richtextbox1.text.contains("Tank") Then 
    Tank.ForeColor = ForeColor.red 
End If 

我只是想单词“坦克”是红色的。

+0

好了,会不会有在RichTextBox内多个实例? – Codexer

+0

提供您尝试过的某些代码示例。 – djv

+0

我有什么东西可以帮到你... – Codexer

回答

0

请试试下面的例子。

Public Sub ColorText(box As RichTextBox, color As Color) 
    box.[Select](start, 5) 
    box.SelectionColor = color 
End Sub 
+1

阅读标签。 VB.NET,而不是C#。 – user3326190

+0

这也行不通,你究竟选择了什么? – Codexer

+2

@ user3326190而不是downvoting,你应该能够使用在线.NET代码转换器翻译3行C#代码。 – ElektroStudios

0

修订答:

我已经写了使用正则表达式与能够使用正则表达式模式找话的优势的解决方案,具有良好的功能,如CASEINSENSITIVE,前景色,背景色,字体,并找到多个单词。

用法示例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 
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 

下面是截图我的输出给你看......

Screenshot

+0

如果您看到以下内容,请区分大小写... – Codexer

相关问题