Green Fire
Green Fire

Reputation: 709

Find and color all occurences of a String in a RichTextBox

I am using a RichTextBox Control,

I want to make a function That Receives String, Color and RichTextBox (obj), then the function will Color all instances of the String that the function Receives.

I tried to make this function but I got a bad Results.

My function head is :

Public Sub ColorWord(ByVal T As RichTextBox, ByVal Word As String, ByVal color1 As Color)

For example :

            RichTextBox1.text = xxxxxtextxxxxxtextxxx

            After Runing the function like this :
            ColorWord(RichTextBox1,"text",Color.red)

The result should be :

xxxxxtextxxxxxtextxxx

Upvotes: 1

Views: 4164

Answers (2)

Dion Terry
Dion Terry

Reputation: 1

Public Enum Highlight_Type
    Forecolor
    Backcolor
End Enum
Public Sub Reset_Highlight(ByVal T As RichTextBox, ByVal HighlightColorReset As Color, ByVal Type As Highlight_Type)
    On Error Resume Next
    T.SelectAll()
    Select Case Type
        Case Highlight_Type.Backcolor
            T.SelectionBackColor = HighlightColorReset
        Case Highlight_Type.Forecolor
            T.SelectionColor = HighlightColorReset
    End Select
    T.HideSelection = True
End Sub
Public Sub HighLight(ByVal T As RichTextBox, ByVal Word As String, ByVal HighlightColor As Color, ByVal HighlightColorReset As Color, ByVal Type As Highlight_Type)
    On Error Resume Next
    Reset_Highlight(T, HighlightColorReset, Type)
    If Word = Nothing Then Exit Sub
    Dim pos As Integer = 0
    Dim s As String = T.Text
    Dim i As Integer = 0
    Dim StopWhile As Boolean = False
    While Not StopWhile
        Dim j As Integer = s.IndexOf(Word, i)
        If j < 0 Then
            StopWhile = True
        Else
            T.Select(j, Word.Length)
            Select Case Type
                Case Highlight_Type.Backcolor
                    T.SelectionBackColor = HighlightColor
                Case Highlight_Type.Forecolor
                    T.SelectionColor = HighlightColor
            End Select
            i = j + 1
        End If
    End While
    T.Select(pos, 0)
End Sub

Upvotes: -2

sara
sara

Reputation: 3934

static void Highlight(RichTextBox box, string word , Color color) {
  int pos =0;
  string s = box.Text;
  for (int i = 0; ; ) {
    int j = s.IndexOf(word , i, StringComparison.CurrentCultureIgnoreCase);
    if (j < 0) break;
    box.SelectionStart = j;
    box.SelectionLength = word .Length;
    box.SelectionColor = color;
    i = j + 1;
  }
  box.SelectionStart = pos;
  box.SelectionLength = 0;
}

Converted Code to VB.net :

Public Sub hl(ByVal T As RichTextBox, ByVal Word As String, ByVal color1 As Color)
       Dim pos As Integer = 0
       Dim s As String = T.Text
       Dim i As Integer = 0
       Dim StopWhile As Boolean = False
       While Not StopWhile
           Dim j As Integer = s.IndexOf(Word, i)
           If j < 0 Then
               StopWhile = True
           Else
               T.Select(j, Word.Length)
               T.SelectionColor = color1
               i = j + 1
           End If
       End While
       T.Select(pos, 0)
   End Sub

Upvotes: 4

Related Questions