What's Here?
- Members: 340,103
- Replies: 920,374
- Topics: 154,922
- Snippets: 4,854
- Tutorials: 1,257
- Total Online: 4,755
- Members: 136
- Guests: 4,619
|
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!
Chat LIVE With a Expert
|
adding replace method to text editor
adding replace method to text editor
Rate Topic:
   

- New D.I.C Head
-
-
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

- apt-get install DIC.bin
-
-
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.

- New D.I.C Head
-
-
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

- apt-get install DIC.bin
-
-
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

- New D.I.C Head
-
-
Group:
Members
-
Posts:
32
-
Joined:
27-February 09
Dream Kudos: 0
Posted 10 April 2009 - 09:43 AM
PsychoCoder, 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

- New D.I.C Head
-
-
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
1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|