Join 306,817 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,704 people online right now. Registration is fast and FREE... Join Now!
Hi, I wanted to know how to add a replace method to a text editor (notepad) to search for a word and replace it in visual basic 2005 1. say in (notepad) you type something in the textbox1-------"me" 2. then am suppose to type "me" in text box 2---and 3. click the find button and it suppose to find the text in textbox1 4. then you type in textbox3---- "us" and 5. click the replace button and it replaces "me" with "us"
how do i do this?
This post has been edited by lostgirl21: 9 Apr, 2009 - 09:42 PM
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
this is what i have for the replay but i don't know what to do for the replace button ...i just don't know how to code for the find button and link it to the replace button
CODE
Private Sub xReplaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xReplaceButton.Click xRichTextBox1.Text = Replace(xReplaceTextBox.Text, "find", "replace with") End Sub End Sub
This post has been edited by lostgirl21: 10 Apr, 2009 - 08:55 AM
Here's an example of a find and replace function that may be helpful for you
CODE
Public Shared Sub FindAndReplace(ByRef lookFor As String, ByRef replaceWith As String, ByRef matchCase As Boolean, ByVal rtb As RichTextBox) Try If rtb.SelectedText.Length <> 0 Then rtb.SelectedText = replaceWith End If
Dim startPos As Integer Dim type As StringComparison type = IIf(matchCase = True,StringComparison.Ordinal,StringComparison.OrdinalIgnoreCase)
startPos = rtb.Text.IndexOf(lookFor, type)
If startPos = 0 OrElse startPos < 0 Then MessageBox.Show("Search text: '" + lookFor + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) Return End If
rtb.Select(startPos, lookFor.Length) rtb.ScrollToCaret() Catch ex As Exception MessageBox.Show(ex.Message, "Find & Replace Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End Try End Sub
If you wanted to implement a "Find all and replace" option you could do it like this
CODE
Public Shared Sub FindAndReplaceAll(ByRef lookFor As String, ByRef replaceWith As String, ByRef matchCase As Boolean, ByVal rtb As RichTextBox) Try Dim type As StringComparison
type = IIf(matchCase = True,StringComparison.Ordinal,StringComparison.OrdinalIgnoreCase)
Dim reg As New Regex("\b" + lookFor + "\b") Dim mc As MatchCollection = reg.Matches(rtb.Rtf)
rtb.Rtf = rtb.Rtf.Replace(lookFor.Trim(), replaceWith.Trim()) Dim startPos As Integer
Dim msg As String = String.Format("{0} occurrences replaced", mc.Count) MessageBox.Show(msg, "Replace Results", MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.Message, "Find & Replace Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End Try End Sub
Here's an example of a find and replace function that may be helpful for you
CODE
Public Shared Sub FindAndReplace(ByRef lookFor As String, ByRef replaceWith As String, ByRef matchCase As Boolean, ByVal rtb As RichTextBox) Try If rtb.SelectedText.Length <> 0 Then rtb.SelectedText = replaceWith End If
Dim startPos As Integer Dim type As StringComparison type = IIf(matchCase = True,StringComparison.Ordinal,StringComparison.OrdinalIgnoreCase)
startPos = rtb.Text.IndexOf(lookFor, type)
If startPos = 0 OrElse startPos < 0 Then MessageBox.Show("Search text: '" + lookFor + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) Return End If
rtb.Select(startPos, lookFor.Length) rtb.ScrollToCaret() Catch ex As Exception MessageBox.Show(ex.Message, "Find & Replace Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End Try End Sub
If you wanted to implement a "Find all and replace" option you could do it like this
CODE
Public Shared Sub FindAndReplaceAll(ByRef lookFor As String, ByRef replaceWith As String, ByRef matchCase As Boolean, ByVal rtb As RichTextBox) Try Dim type As StringComparison
type = IIf(matchCase = True,StringComparison.Ordinal,StringComparison.OrdinalIgnoreCase)
Dim reg As New Regex("\b" + lookFor + "\b") Dim mc As MatchCollection = reg.Matches(rtb.Rtf)
rtb.Rtf = rtb.Rtf.Replace(lookFor.Trim(), replaceWith.Trim()) Dim startPos As Integer
Dim msg As String = String.Format("{0} occurrences replaced", mc.Count) MessageBox.Show(msg, "Replace Results", MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.Message, "Find & Replace Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End Try End Sub
Hope that helps
Thanks that helps alot...i just have to break it down in sections...one for find all....then one for replace..then another for replace all
i've used this for the replace all method....now i dunno how to use it to change it into the replace one word...
CODE
any ideas?Private Sub xReplaceAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xReplaceAllButton.Click xTxtSource.Text = Replace(xTxtSource.Text, Me.xFindTextBox.Text, Me.xReplaceTextBox.Text) End Sub