title: Find And Format String in RichTextBox Vb.NET
body: <p>I am working with Vb.NET and the requirement was to find the string n RTF control and make it , bold,italic or whatever color,I have done my efforts to find the string but yesterday I found it not working as per my requirement.</p>
  
  <p>At bottom I will write the full code with test data.</p>
  
  <p>I have a simple form and two control on it, One is CombBox to Select Different Test Cases and One RichTextBox for Displaying the Text.</p>
  
  <pre><code> 'Class Declarations
  
    Private FormattingApplied As Boolean = False
    Private SelectedBold As Boolean = False
    Private SearchText As String = String.Empty
    Private SelectedItalic As Boolean = False
    Private SelectedUnderLine As Boolean = False
    ' On Form Load
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      cmbList.Items.Add("Apple Pina Apple")
      cmbList.Items.Add("Apple Delicious Pineapple")
      cmbList.Items.Add("Apple Milk Shake Apa")
      cmbList.Items.Add("Apple Strawberry ")
      FormattingApplied = True
      SelectedBold = True
      cmbList.SelectedIndex = 0
   End Sub
   Function UppercaseFirstLetter(ByVal val As String) As String
      ' Test for nothing or empty.
      If String.IsNullOrEmpty(val) Then
          Return val
      End If
      Dim array() As Char = val.ToCharArray
  
      array(0) = Char.ToUpper(array(0))
      Return New String(array)
  End Function
  Private Sub ApplyFormatting(ByVal SearchText As String)
      Dim TrimmedString As String = String.Empty
      Dim ISTrimmed As Boolean = False
      If FormattingApplied Then
          Dim count As New List(Of Integer)()
          If rtfText.Text.Length &gt;= 53 Then
              rtfText.Text = rtfText.Text.Substring(0, 50) + "..."
          End If
  
          For i As Integer = 0 To rtfText.Text.Length - 1
              If rtfText.Text.IndexOf(SearchText, i) &lt;&gt; -1 Then
                  count.Add(rtfText.Text.IndexOf(SearchText, i))
              ElseIf rtfText.Text.IndexOf(UppercaseFirstLetter(SearchText), i) &lt;&gt; -1 Then
                  count.Add(rtfText.Text.IndexOf(UppercaseFirstLetter(SearchText), i))
              End If
          Next
          Try
              For i As Integer = 0 To count.Count - 1
                  rtfText.[Select](count(i), SearchText.Length)
                  If SelectedBold Then
                      rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Bold)
                  ElseIf SelectedItalic Then
                      rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Italic)
                  ElseIf SelectedUnderLine Then
                      rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Underline)
                      'ElseIf SelectedBold AndAlso SelectedItalic AndAlso SelectedUnderLine Then
                      '    rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Underline)
                  End If
                  count.RemoveAt(i)
              Next
              'For i As Integer = 0 To count.Count - 1
              '    rtfText.[Select](count(i), SearchText.Length)
              '    If SelectedBold Then
              '        rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Bold)
              '    ElseIf SelectedItalic Then
              '        rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Italic)
              '    ElseIf SelectedUnderLine Then
              '        rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Underline)
              '    End If
              '    count.RemoveAt(i)
              'Next
          Catch
              count.Reverse()
          End Try
          rtfText.[Select](rtfText.Text.Length, 0)
          rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Regular)
      End If
  End Sub
  
   Private Sub cmbList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbList.SelectedIndexChanged
      rtfText.Text = cmbList.SelectedItem.ToString()
      rtfText.[Select](0, rtfText.Text.Length)
      rtfText.SelectionFont = New Font(rtfText.Font, FontStyle.Regular)
      If cmbList.SelectedIndex &gt; -1 Then
          ApplyFormatting("apple")
      End If
  End Sub
  </code></pre>
  
  <p>Here is the output:
  <a href="https://i.stack.imgur.com/hRgDT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hRgDT.png" alt="Output"></a></p>