Public Class Form1
    Dim i As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.SelectionStart = 0
        RichTextBox1.SelectionLength = Len(RichTextBox1.Text)
        RichTextBox1.SelectionColor = Color.Black

        If (CheckBox1.Checked = False And CheckBox2.Checked = False) Then
            i = RichTextBox1.Find(TextBox1.Text)
            If i = -1 Then
                MsgBox("Nu s-a gasi textul cautat!", 16)
            End If
            Do While i >= 0
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red
                i = RichTextBox1.Find(TextBox1.Text, i + Len(TextBox1.Text), Len(RichTextBox1.Text) + 1, RichTextBoxFinds.None)
            Loop
        End If

        If (CheckBox1.Checked = True And CheckBox2.Checked = False) Then
            i = RichTextBox1.Find(TextBox1.Text, RichTextBoxFinds.MatchCase)
            Do While i >= 0
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red
                i = RichTextBox1.Find(TextBox1.Text, i + Len(TextBox1.Text), Len(RichTextBox1.Text) + 1, RichTextBoxFinds.MatchCase)
            Loop
        End If

        If (CheckBox1.Checked = False And CheckBox2.Checked = True) Then
            i = RichTextBox1.Find(TextBox1.Text, RichTextBoxFinds.Reverse)
            If i > -1 Then
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red

            End If
        End If

        If (CheckBox1.Checked = True And CheckBox2.Checked = True) Then
            i = RichTextBox1.Find(TextBox1.Text, RichTextBoxFinds.MatchCase Or RichTextBoxFinds.Reverse)
            If i > -1 Then
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red
            End If
        End If
    End Sub
End Class
