Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- If Richtextbox1.text.contains("Tank") Then
- Tank.ForeColor = ForeColor.red
- End If
- 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
- Public Sub ColorText(box As RichTextBox, color As Color)
- box.[Select](start, 5)
- box.SelectionColor = color
- End Sub
- ColorizeWord(RichTextBox1, "Tank", True,
- Color.Red, Color.Black,
- New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic))
- ColorizeWords(RichTextBox1, {"Tank", "[0-9]"},
- IgnoreCase:=False,
- ForeColor:=Color.Red, BackColor:=Nothing, Font:=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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement