If you didn't understand what i just asked just try looking at it a different way.
Find and Replace problem
Page 1 of 1
Find and Replace problem
#1
Posted 27 October 2009 - 06:12 PM
Ok so i have got the code for a notepads find and replace, but i cant seem to get a "Replace All" button to work. So i made a timer and put my find and replace code in it, put interval to 1, made it so when you press "Replace All" , timer1 enables. So heres the problem, i want timer1 to replace the text up to untill it is all replaced.
If you didn't understand what i just asked just try looking at it a different way.
If you didn't understand what i just asked just try looking at it a different way.
#2
Re: Find and Replace problem
Posted 27 October 2009 - 07:42 PM
Is there a part in your Find and Replace code where you handle if what you are looking for is not found? What you could do is create a function that would return a Boolean True or False if the Find and Replace was successful. Then in a do while loop for the click event of the Replace All button you could try something like this:
The call to FindAndReplace would be the function that would try to find and replace a word. The wordToFind parameter would be the word to find and wordToReplace would be the word to replace with.
Dim ResultFound As Boolen ResultFound = True Do While ResultFound = True ResultFound = FindAndReplace(wordToFind, wordToReplace) Loop
The call to FindAndReplace would be the function that would try to find and replace a word. The wordToFind parameter would be the word to find and wordToReplace would be the word to replace with.
#3
Re: Find and Replace problem
Posted 27 October 2009 - 08:29 PM
No need to use a Timer control for something like this. You could do something like this
This example uses a RichTextBox (which to me is preferred over a regular TextBox)
Public Sub FindAndReplaceAll(ByRef lookFor As String, ByRef replaceWith As String, ByRef matchCase As Boolean, rtb As RichTextBox)
Try
Dim type As StringComparison
type = Iif(matchCase, 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
startPos = rtb.Text.IndexOf(lookFor, 0, type)
rtb.Select(startPos, replaceWith.Length)
rtb.ScrollToCaret()
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
This example uses a RichTextBox (which to me is preferred over a regular TextBox)
#4
Re: Find and Replace problem
Posted 28 October 2009 - 03:52 PM
PsychoCoder, on 27 Oct, 2009 - 08:29 PM, said:
No need to use a Timer control for something like this. You could do something like this
This example uses a RichTextBox (which to me is preferred over a regular TextBox)
Public Sub FindAndReplaceAll(ByRef lookFor As String, ByRef replaceWith As String, ByRef matchCase As Boolean, rtb As RichTextBox)
Try
Dim type As StringComparison
type = Iif(matchCase, 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
startPos = rtb.Text.IndexOf(lookFor, 0, type)
rtb.Select(startPos, replaceWith.Length)
rtb.ScrollToCaret()
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
This example uses a RichTextBox (which to me is preferred over a regular TextBox)
i Copied + Pasted code and show a couple of errors like MatchCase is not Valid, or Matchcollection is not valid
#6
Re: Find and Replace problem
Posted 27 November 2009 - 09:53 PM
Well my find code is:
and my replace code is:
So does that help because the other codes did not make sense.
Private Sub Find() Form2.RichTextBox1.Find(TextBox1.Text.ToString, RichTextBoxFinds.MatchCase) End Sub
and my replace code is:
Private Sub Replace() Form2.RichTextBox1.SelectedText = TextBox2.Text End Sub
So does that help because the other codes did not make sense.
Page 1 of 1

Add Reply





MultiQuote

| 


