Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's some code I wrote to do a find and replace.

There's a textbox for find "tbxFind", and one for replace "tbxReplace".
When I execute the code, it finds all of the occurrences and replaces them all.

What I'd like to be able to do is find each occurrence one at a time, and replace it one at a time.
=========================================================================
VB
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
      tbxEditor.Text = Replace(tbxEditor.Text, tbxFind.Text, tbxReplace.Text)
      MsgBox(" ' " & tbxFind.Text & " ' " & " Has been replaced with:" & " ' " & tbxReplace.Text & " ' ")
   End Sub




Thanks
Posted
Updated 8-Dec-10 21:55pm
v3
Comments
Dalek Dave 9-Dec-10 3:55am    
Edited for Readability.

1 solution

You could use Remove and Insert, like so:
VB
Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click

        Dim iplace As String = tbxEditor.Text.IndexOf(tbxFind.Text)
        tbxEditor.Text = tbxEditor.Text.Remove(iplace, Len(tbxFind.Text)).Insert(iplace, tbxReplace.Text)

End Sub

This way, replacement is one by one on buttonclick.
 
Share this answer
 
v2
Comments
Dalek Dave 9-Dec-10 3:55am    
Good Answer.
Rajesh Anuhya 9-Dec-10 4:01am    
Good :) +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900