VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,331 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,807 people online right now. Registration is fast and FREE... Join Now!




adding replace method to text editor

 

adding replace method to text editor

lostgirl21

9 Apr, 2009 - 09:40 PM
Post #1

New D.I.C Head
*

Joined: 27 Feb, 2009
Posts: 32

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

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Adding Replace Method To Text Editor

10 Apr, 2009 - 06:54 AM
Post #2

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,701



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

lostgirl21

RE: Adding Replace Method To Text Editor

10 Apr, 2009 - 08:49 AM
Post #3

New D.I.C Head
*

Joined: 27 Feb, 2009
Posts: 32

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
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Adding Replace Method To Text Editor

10 Apr, 2009 - 09:01 AM
Post #4

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,701



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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

        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 smile.gif
User is offlineProfile CardPM
+Quote Post

lostgirl21

RE: Adding Replace Method To Text Editor

10 Apr, 2009 - 09:43 AM
Post #5

New D.I.C Head
*

Joined: 27 Feb, 2009
Posts: 32

QUOTE(PsychoCoder @ 10 Apr, 2009 - 09:01 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

        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 smile.gif

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
User is offlineProfile CardPM
+Quote Post

lostgirl21

RE: Adding Replace Method To Text Editor

10 Apr, 2009 - 02:28 PM
Post #6

New D.I.C Head
*

Joined: 27 Feb, 2009
Posts: 32

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

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 04:21PM

Live VB.NET Help!

Be Social

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

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month