School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,103 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,755 people online right now. Registration is fast and FREE... Join Now!



adding replace method to text editor

Page 1 of 1

adding replace method to text editor Rate Topic: -----

#1 lostgirl21  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 32
  • Joined: 27-February 09


Dream Kudos: 0

Posted 09 April 2009 - 09:40 PM

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: 09 April 2009 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -


#2 PsychoCoder  Icon User is offline

  • apt-get install DIC.bin
  • Icon
  • View blog
  • Group: Admins
  • Posts: 16,211
  • Joined: 26-July 07


Dream Kudos: 12400

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

Posted 10 April 2009 - 06:54 AM

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.

Please post like this:

Thank you for helping us helping you.
Was This Post Helpful? 0
  • +
  • -

#3 lostgirl21  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 32
  • Joined: 27-February 09


Dream Kudos: 0

Posted 10 April 2009 - 08:49 AM

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

	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 April 2009 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • apt-get install DIC.bin
  • Icon
  • View blog
  • Group: Admins
  • Posts: 16,211
  • Joined: 26-July 07


Dream Kudos: 12400

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

Posted 10 April 2009 - 09:01 AM

Here's an example of a find and replace function that may be helpful for you

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

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

		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



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#5 lostgirl21  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 32
  • Joined: 27-February 09


Dream Kudos: 0

Posted 10 April 2009 - 09:43 AM

View PostPsychoCoder, on 10 Apr, 2009 - 09:01 AM, said:

Here's an example of a find and replace function that may be helpful for you

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

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

		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



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

#6 lostgirl21  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 32
  • Joined: 27-February 09


Dream Kudos: 0

Posted 10 April 2009 - 02:28 PM

i've used this for the replace all method....now i dunno how to use it to change it into the replace one word...
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


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