Hi all.
im writing function to find and replace the string. Im using 2 textbox's one for keyword which is to find and one to to replace. im having having two buttons btnfindnext and btnreplace. if i click on replace button it should select the text first and agin if i click on the same it should replace the string. and if i click on findnext it should select the next keyword in the document and if again btn replace is clicked it should replace the selected text.
Like in Ms word

thanks

Recommended Answers

All 5 Replies

Try this:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox3.Text = "The quick brown fox jumps over the lazy dog."
    End Sub

    Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
        Dim idx As Integer = 0
        idx = TextBox3.Text.IndexOf(TextBox1.Text, idx)
        If idx = -1 Then
            MessageBox.Show(TextBox1.Text & " is not in Textbox3")
        Else
            TextBox3.SelectionStart = idx
            TextBox3.SelectionLength = TextBox1.Text.Length
            TextBox3.Focus()
        End If
    End Sub

    Private Sub ReplaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceButton.Click
        If TextBox3.Text.Contains(TextBox1.Text) Then
            TextBox3.Text = TextBox3.Text.Replace(TextBox1.Text, TextBox2.Text)
        Else
            MessageBox.Show(TextBox1.Text & " is not in Textbox3")
        End If
    End Sub

End Class

It has 3 textboxes and 2 buttons.

Thnks wayne,
But ur code will search only for fisrt instance of keword and if i click on replace it is replaceing all the instance of key word. if i keep on clicking findnext button it should select and highlight the keyword untill txtend is reached. and when user click on replcae it has to replace the selected text and also it should highlight the next instance of keword in the document if there.
I think its clear to you. please help me out.

Hi,
To search all the instance of keyword, you should explicitly save the last search index.

For this u can use the Variable

Dim iLastIndex as Integer

    Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
        Dim idx As Integer = iLastIndex
        idx = TextBox3.Text.IndexOf(TextBox1.Text, idx)
        If idx = -1  Then
            MessageBox.Show(TextBox1.Text & " is not in Textbox3")
        Else
            TextBox3.SelectionStart = idx
            TextBox3.SelectionLength = TextBox1.Text.Length
            TextBox3.Focus()
            iLastIndex = TextBox1.Text.Length + idx
        End If
    End Sub

To replace, First u have to select the Text first then replace it.

Dim idx As Integer = iLastIndex
        idx = TextBox3.Text.IndexOf(TextBox1.Text, idx)
        If idx = -1 Then
            MessageBox.Show(TextBox1.Text & " is not in Textbox3")
        Else
            TextBox3.SelectionStart = idx
            TextBox3.SelectionLength = TextBox1.Text.Length
            TextBox3.Focus()
            iLastIndex = idx + TextBox2.Text.Length
            TextBox3.SelectedText = TextBox2.Text
            TextBox3.SelectionStart = idx
            TextBox3.SelectionLength = TextBox2.Text.Length
        End If

The same code to find and Last Three lines for replace.

Also at one stage find will go to end of the file at that time you should reset the iLastIndex variable to 0

Thank you.
But your code is replacing the next keyword not selected keyword.

the code is correct but it will not highlight the similer text present two time in sentenc.mean that onece it find the text the same text is enlighted for next time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.