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
        i = InStr(RichTextBox1.Text, TextBox1.Text)
        If i = 0 Then
            MsgBox("Nu s-a gasit textul cautat!", 16)
        Else
            If RadioButton1.Checked = True Then
                Do While i <> 0

                    RichTextBox1.SelectionStart = i - 1
                    RichTextBox1.SelectionLength = Len(TextBox1.Text)
                    RichTextBox1.SelectionColor = Color.Red
                    i = InStr(i + Len(TextBox1.Text) - 1, RichTextBox1.Text, TextBox1.Text, CompareMethod.Binary)

                Loop
            End If
            If RadioButton2.Checked = True Then
                Do While i <> 0

                    RichTextBox1.SelectionStart = i - 1
                    RichTextBox1.SelectionLength = Len(TextBox1.Text)
                    RichTextBox1.SelectionColor = Color.Red
                    i = InStr(i + Len(TextBox1.Text) - 1, RichTextBox1.Text, TextBox1.Text, CompareMethod.Text)
                Loop
            End If
            End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If RichTextBox1.Text = "" Or TextBox1.Text = "" Or RadioButton1.Checked = False Or RadioButton2.Checked = False Then
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        If RichTextBox1.Text = "" Or TextBox1.Text = "" Or (RadioButton1.Checked = False And RadioButton2.Checked = False) Then
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If RichTextBox1.Text = "" Or TextBox1.Text = "" Or (RadioButton1.Checked = False And RadioButton2.Checked = False) Then
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If (RadioButton1.Checked = True) Then
            If RichTextBox1.Text = "" Or TextBox1.Text = "" Then
                Button1.Enabled = False
            Else
                Button1.Enabled = True
            End If
        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If (RadioButton2.Checked = True) Then
            If RichTextBox1.Text = "" Or TextBox1.Text = "" Then
                Button1.Enabled = False
            Else
                Button1.Enabled = True
            End If
        End If
    End Sub
End Class
