School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

Join 358,767 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,810 people online right now.Registration is fast and FREE... Join Now!



Find and Replace problem

52 Weeks of Code Challenge:Android

Week #11 of the 52 Weeks of Code Challenge is Android, you should give it a shot. Click Here!
Page 1 of 1

Find and Replace problem Rate Topic: -----

#1 MysteriousFeez  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-September 09


Dream Kudos: 0

Post icon  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. ;)
Was This Post Helpful? 0
  • +
  • -


#2 SixOfEleven  Icon User is offline

  • 1-888-611-RULZ
  • Icon

Reputation: 273
  • View blog
  • Posts: 4,374
  • Joined: 18-October 08


Dream Kudos: 1100

Expert In: C, C#, XNA, Game Programming, Chocolate

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:

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.
Was This Post Helpful? 1
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • apt-get install DIC.bin
  • Icon

Reputation: 731
  • View blog
  • Posts: 16,951
  • Joined: 26-July 07


Dream Kudos: 12450

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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

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)
Was This Post Helpful? 0
  • +
  • -

#4 MysteriousFeez  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-September 09


Dream Kudos: 0

Re: Find and Replace problem

Posted 28 October 2009 - 03:52 PM

View PostPsychoCoder, 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

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
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • Icon

Reputation: 134
  • View blog
  • Posts: 676
  • Joined: 25-September 09


Dream Kudos: 25

Re: Find and Replace problem

Posted 28 October 2009 - 03:57 PM

add the following import:

Imports System.Text.RegularExpressions


Was This Post Helpful? 0
  • +
  • -

#6 MysteriousFeez  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-September 09


Dream Kudos: 0

Re: Find and Replace problem

Posted 27 November 2009 - 09:53 PM

Well my find code is:
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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month