Public Class Form1
    Dim i As Integer
    Dim poz As Integer = -1, lung = -1
    Dim flag1 As Integer = 0

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If poz <> RichTextBox1.SelectionLength + RichTextBox1.SelectionStart Then
            poz = RichTextBox1.SelectionLength + RichTextBox1.SelectionStart
            lung = RichTextBox1.SelectionLength
            flag1 = 0
        End If

        RichTextBox1.SelectionStart = 0
        RichTextBox1.SelectionLength = Len(RichTextBox1.Text)
        RichTextBox1.SelectionColor = Color.Black

        If RadioButton1.Checked = False And RadioButton2.Checked = False And RadioButton3.Checked = False And RadioButton4.Checked = False Then
            i = RichTextBox1.Find(TextBox1.Text, 0, Len(RichTextBox1.Text), RichTextBoxFinds.None)
            Do While i <> -1
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red
                i = RichTextBox1.Find(TextBox1.Text, i + 1, Len(RichTextBox1.Text), RichTextBoxFinds.None)
            Loop
        End If


        If RadioButton1.Checked = True And RadioButton3.Checked = True Then
            If flag1 = 0 Then
                If poz + Len(TextBox1.Text) <= Len(RichTextBox1.Text) Then
                    i = RichTextBox1.Find(TextBox1.Text, poz, Len(RichTextBox1.Text), RichTextBoxFinds.MatchCase)
                Else
                    MsgBox("Sirul cautat nu a fost gasit!", 16)
                End If
            Else
                If i + Len(TextBox1.Text) <= Len(RichTextBox1.Text) Then
                    i = RichTextBox1.Find(TextBox1.Text, i + 1, Len(RichTextBox1.Text), RichTextBoxFinds.MatchCase)
                Else
                    MsgBox("Cautarea a ajuns la final!", 16)
                End If
            End If
            If i = -1 Then
                MsgBox("Cautarea a ajuns la final!", 32)
                flag1 = 0
            End If
            If i <> -1 Then
                flag1 = 1
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red
            End If
        End If

        If RadioButton2.Checked = True And RadioButton3.Checked = True Then
            If flag1 = 0 Then
                If poz + Len(TextBox1.Text) <= Len(RichTextBox1.Text) Then
                    i = RichTextBox1.Find(TextBox1.Text, poz, Len(RichTextBox1.Text), RichTextBoxFinds.None)
                Else
                    MsgBox("Sirul cautat nu a fost gasit!", 16)
                End If
            Else
                If i + Len(TextBox1.Text) <= Len(RichTextBox1.Text) Then
                    i = RichTextBox1.Find(TextBox1.Text, i + 1, Len(RichTextBox1.Text), RichTextBoxFinds.None)
                Else
                    MsgBox("Cautarea a ajuns la final!", 16)
                End If
            End If
            If i = -1 Then
                MsgBox("Cautarea a ajuns la final!", 32)
                flag1 = 0
            End If
            If i <> -1 Then
                flag1 = 1
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red
            End If
        End If

        If RadioButton1.Checked = True And RadioButton4.Checked = True Then
            If flag1 = 0 Then
                If poz - Len(TextBox1.Text) >= 0 Then
                    i = RichTextBox1.Find(TextBox1.Text, 0, poz, RichTextBoxFinds.MatchCase Or RichTextBoxFinds.Reverse)
                    flag1 = 1
                Else
                    MsgBox("Sirul cautat nu a fost gasit!", 16)
                End If
            Else
                If i - Len(TextBox1.Text) >= 0 Then
                    i = RichTextBox1.Find(TextBox1.Text, 0, i - 1, RichTextBoxFinds.MatchCase Or RichTextBoxFinds.Reverse)
                    flag1 = 1
                Else
                    MsgBox("Cautarea a ajuns la final!", 16)
                    flag1 = 0
                End If
            End If
            If i = -1 Then
                MsgBox("Cautarea a ajuns la final!", 32)
                flag1 = 0
            End If
            If i <> -1 Then
                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red
            End If
        End If

        If RadioButton2.Checked = True And RadioButton4.Checked = True Then
            If flag1 = 0 Then
                If poz - Len(TextBox1.Text) >= 0 Then
                    i = RichTextBox1.Find(TextBox1.Text, 0, poz, RichTextBoxFinds.None Or RichTextBoxFinds.Reverse)
                    flag1 = 1
                Else
                    MsgBox("Sirul cautat nu a fost gasit!", 16)
                End If
            Else
                If i - Len(TextBox1.Text) >= 0 Then
                    i = RichTextBox1.Find(TextBox1.Text, 0, i - 1, RichTextBoxFinds.None Or RichTextBoxFinds.Reverse)
                    flag1 = 1
                Else
                    MsgBox("Cautarea a ajuns la final!", 16)
                    flag1 = 0
                End If
            End If
            If i = -1 Then
                MsgBox("Cautarea a ajuns la final!", 32)
                flag1 = 0
            End If
            If i <> -1 Then

                RichTextBox1.SelectionStart = i
                RichTextBox1.SelectionLength = Len(TextBox1.Text)
                RichTextBox1.SelectionColor = Color.Red
            End If
        End If
        RichTextBox1.SelectionStart = poz - lung
        RichTextBox1.SelectionLength = lung

    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 = "" Then
            Button1.Enabled = False
            Button2.Enabled = False
            Button3.Enabled = False
        End If
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        If Not (RichTextBox1.Text = "" Or TextBox1.Text = "") Then
            Button1.Enabled = True
            Button2.Enabled = True
            Button3.Enabled = True
        End If
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not (RichTextBox1.Text = "" Or TextBox1.Text = "") Then
            Button1.Enabled = True
            Button2.Enabled = True
            Button3.Enabled = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        i = RichTextBox1.Find(TextBox1.Text, 0, Len(RichTextBox1.Text), RichTextBoxFinds.None)
        If i <> -1 Then
            RichTextBox1.Text = RichTextBox1.Text.Insert(i, TextBox2.Text)
            RichTextBox1.Text = RichTextBox1.Text.Remove(i + Len(TextBox2.Text), Len(TextBox1.Text))

            i = RichTextBox1.Find(TextBox1.Text, i + Len(TextBox2.Text), Len(RichTextBox1.Text), RichTextBoxFinds.None)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RichTextBox1.Text = RichTextBox1.Text.Replace(TextBox1.Text, TextBox2.Text)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub
End Class
