How do you replace all instances in a richtextbox?

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts Page 1 of 1
Contributors
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Hello,

How can I make my code (below) replace all instances of the given text, instead of just one instance?

Code:
Code: Select all
        'Find and replace
        Try
            Dim strFind As String = InputBox("Enter string you want to replace. Enter 'No' if you don't want to find only whole words.", "Find")
            If strFind = "No" Then
                Dim strFind2 As String = InputBox("Enter string you want to replace.", "Find")
                Dim strReplace As String = InputBox("Enter string you want to replace with.", "Replace with")
                RichTextBox1.Find(strFind2, RichTextBoxFinds.None)
                RichTextBox1.SelectedText = strReplace
            Else
                Dim strReplace2 As String = InputBox("Enter string you want to replace with.", "Replace with")
                RichTextBox1.Find(strFind, RichTextBoxFinds.WholeWord)
                RichTextBox1.SelectedText = strReplace2
            End If
        Catch ex As Exception
            MsgBox(ex)
        End Try
Please help!

~GoodGuy17 :D
User avatar
Zulf
Serious Programmer
Serious Programmer
Posts: 441
Joined: Fri Jun 11, 2010 7:46 am

Code: Select all
RichTextBox1.Text.Replace(original, new)
Image
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Didn't work. It didn't replace anything.
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Put this under Replace All button click event
Code: Select all
Dim cp As Integer = RichTextBox1.SelectionStart
        Dim cs As Integer = RichTextBox1.SelectionLength

        RichTextBox1.Rtf = Replace(RichTextBox1.Rtf, Trim(TextBox1.Text), Trim(TextBox2.Text))
        RichTextBox1.SelectionStart = cp
        RichTextBox1.SelectionLength = cs

        Me.Focus()
In this code -
TextBox1 - The text which you want to replace
TextBox2 - The text with which you want the TextBox1 text to be replaced.

If you like my posts please give me +rep!! cooll;
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Thanks CoderBoy! You've got it! cooll;
5 posts Page 1 of 1
Return to “Tutorial Requests”