Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. If Richtextbox1.text.contains("Tank") Then
  2. Tank.ForeColor = ForeColor.red
  3. End If
  4.  
  5. Private Function FormatText(ByVal TextToFormat As String, ByVal TextFormat As Integer, ByVal TextColor As Color)
  6. Dim count As New List(Of Integer)()
  7. For i As Integer = 0 To rText.Text.Length - 1
  8. If rText.Text.IndexOf(TextToFormat, i) <> -1 Then
  9. 'If the word is found add the index to the list
  10. count.Add(rText.Text.IndexOf(TextToFormat, i))
  11. End If
  12. Next
  13.  
  14. Try
  15. For i As Integer = 0 To count.Count - 1
  16. rText.[Select](count(i), TextToFormat.Length)
  17. Select Case TextFormat
  18. Case 1
  19. rText.SelectionFont = New Font(rText.Font.FontFamily, rText.Font.Size, FontStyle.Bold)
  20. rText.SelectionColor = TextColor
  21. Case 2
  22. rText.SelectionFont = New Font(rText.Font.FontFamily, rText.Font.Size, FontStyle.Italic)
  23. rText.SelectionColor = TextColor
  24. End Select
  25. count.RemoveAt(i)
  26. Next
  27. Catch
  28. End Try
  29. Return Nothing
  30. End Function
  31.  
  32. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  33. If rText.Text.Contains("Tank") Then
  34. FormatText("Tank", 1, Color.Red)
  35. End If
  36. End Sub
  37.  
  38. Public Sub ColorText(box As RichTextBox, color As Color)
  39. box.[Select](start, 5)
  40. box.SelectionColor = color
  41. End Sub
  42.  
  43. ColorizeWord(RichTextBox1, "Tank", True,
  44. Color.Red, Color.Black,
  45. New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Italic))
  46.  
  47. ColorizeWords(RichTextBox1, {"Tank", "[0-9]"},
  48. IgnoreCase:=False,
  49. ForeColor:=Color.Red, BackColor:=Nothing, Font:=Nothing)
  50.  
  51. ''' <summary>
  52. ''' Find a word on a RichTextBox and colorizes each match.
  53. ''' </summary>
  54. ''' <param name="RichTextBox">Indicates the RichTextBox.</param>
  55. ''' <param name="Word">Indicates the word to colorize.</param>
  56. ''' <param name="IgnoreCase">Indicates the ignore case.</param>
  57. ''' <param name="ForeColor">Indicates the text color.</param>
  58. ''' <param name="BackColor">Indicates the background color.</param>
  59. ''' <param name="Font">Indicates the text font.</param>
  60. ''' <returns><c>true</c> if matched at least one word, <c>false</c> otherwise.</returns>
  61. Private Function ColorizeWord(ByVal [RichTextBox] As RichTextBox,
  62. ByVal Word As String,
  63. Optional ByVal IgnoreCase As Boolean = False,
  64. Optional ByVal ForeColor As Color = Nothing,
  65. Optional ByVal BackColor As Color = Nothing,
  66. Optional ByVal [Font] As Font = Nothing) As Boolean
  67.  
  68. ' Find all the word matches.
  69. Dim Matches As System.Text.RegularExpressions.MatchCollection =
  70. System.Text.RegularExpressions.Regex.Matches([RichTextBox].Text, Word,
  71. If(IgnoreCase,
  72. System.Text.RegularExpressions.RegexOptions.IgnoreCase,
  73. System.Text.RegularExpressions.RegexOptions.None))
  74.  
  75. ' If no matches then return.
  76. If Not Matches.Count <> 0 Then
  77. Return False
  78. End If
  79.  
  80. ' Set the passed Parameter values.
  81. If ForeColor.Equals(Nothing) Then ForeColor = [RichTextBox].ForeColor
  82. If BackColor.Equals(Nothing) Then BackColor = [RichTextBox].BackColor
  83. If [Font] Is Nothing Then [Font] = [RichTextBox].Font
  84.  
  85. ' Store the current caret position to restore it at the end.
  86. Dim CaretPosition As Integer = [RichTextBox].SelectionStart
  87.  
  88. ' Suspend the control layout to work quicklly.
  89. [RichTextBox].SuspendLayout()
  90.  
  91. ' Colorize each match.
  92. For Each Match As System.Text.RegularExpressions.Match In Matches
  93.  
  94. [RichTextBox].Select(Match.Index, Match.Length)
  95. [RichTextBox].SelectionColor = ForeColor
  96. [RichTextBox].SelectionBackColor = BackColor
  97. [RichTextBox].SelectionFont = [Font]
  98.  
  99. Next Match
  100.  
  101. ' Restore the caret position.
  102. [RichTextBox].Select(CaretPosition, 0)
  103.  
  104. ' Restore the control layout.
  105. [RichTextBox].ResumeLayout()
  106.  
  107. ' Return successfully
  108. Return True
  109.  
  110. End Function
  111.  
  112. ''' <summary>
  113. ''' Find multiple words on a RichTextBox and colorizes each match.
  114. ''' </summary>
  115. ''' <param name="RichTextBox">Indicates the RichTextBox.</param>
  116. ''' <param name="Words">Indicates the words to colorize.</param>
  117. ''' <param name="IgnoreCase">Indicates the ignore case.</param>
  118. ''' <param name="ForeColor">Indicates the text color.</param>
  119. ''' <param name="BackColor">Indicates the background color.</param>
  120. ''' <param name="Font">Indicates the text font.</param>
  121. ''' <returns><c>true</c> if matched at least one word, <c>false</c> otherwise.</returns>
  122. Private Function ColorizeWords(ByVal [RichTextBox] As RichTextBox,
  123. ByVal Words As String(),
  124. Optional ByVal IgnoreCase As Boolean = False,
  125. Optional ByVal ForeColor As Color = Nothing,
  126. Optional ByVal BackColor As Color = Nothing,
  127. Optional ByVal [Font] As Font = Nothing) As Boolean
  128.  
  129. Dim Success As Boolean = False
  130.  
  131. For Each Word As String In Words
  132. Success += ColorizeWord([RichTextBox], Word, IgnoreCase, ForeColor, BackColor, [Font])
  133. Next Word
  134.  
  135. Return Success
  136.  
  137. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement