Click here to Skip to main content
6,629,377 members and growing! (18,806 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate License: The Code Project Open License (CPOL)

Find And Replace Method For RichTextBox

By Chatura Dilan

An artcal about richtextbox control
VB 6, Windows, .NET 2.0VS2005, Dev
Posted:19 Mar 2006
Views:31,402
Bookmarked:17 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 3.93 Rating: 3.77 out of 5
3 votes, 27.3%
1

2

3
3 votes, 27.3%
4
5 votes, 45.5%
5

Sample Image - findandriplace_rtb.jpg

Introduction

This article describes how to create a find and replace method for the RichTextBox control. It is  easy to understand. If you develop a word processing application this method will be a grate gadget for you. You can create any method for any control like this.

 

How To Create It

1. First, create a new project and name it "FRRichTextBox"

2. Then we need to create a class RichTextBoxFR.vb. to create it, click on Add New Item button on the Standard Toolbar.

3. Select Class from the template and name it RichTextBoxFR.vb and Click on Add button.

4. We use inherits statement and derive our class from RichTextBox Class

Public Class RichTextBoxFR
    Inherits RichTextBox

End Class

5. We create a FindAndReplace method with two arguments - FindText and ReplaceText.

Public Class RichTextBoxFR
    Inherits RichTextBox
    
    Public Sub FindAndReplace(ByVal FindText As String, ByVal ReplaceText As String)
    Me.Find(FindText)
            If Not Me.SelectionLength = 0 Then
            Me.SelectedText = ReplaceText
            Else
            MsgBox("The following specified text was not found: " & FindText)
            End If
    End Sub
    
End Class

Me here is RichTextBox. We use Me.Find(FindText) to search the text of the RichTextBox control and select the text. If the search text is found, RichTextBox select it and SelectionLength of RichTextBox will be > 0. Then we use if else statement to replace that text as above.

6. Now we create another FindAndReplace method by overloading our previous FindAndReplace method. You can see that method has five arguments - FindText, ReplaceText, ReplaceAll, MatchCase and WholeWord

Public Class RichTextBoxFR
    Inherits RichTextBox
    
    Public Sub FindAndReplace(ByVal FindText As String, ByVal ReplaceText As String)
    Me.Find(FindText)
            If Not Me.SelectionLength = 0 Then
            Me.SelectedText = ReplaceText
            Else
            MsgBox("The following specified text was not found: " & FindText)
            End If
    End Sub
    
    
    Public Sub FindAndReplace(ByVal FindText As String, ByVal ReplaceText As String, ByVal ReplaceAll As Boolean, _
         ByVal MatchCase As Boolean, ByVal WholeWord As Boolean)

            Select Case ReplaceAll

            Case False
                If MatchCase = True Then
                    If WholeWord = True Then
                        Me.Find(FindText, RichTextBoxFinds.MatchCase Or RichTextBoxFinds.WholeWord)
                    Else
                        Me.Find(FindText, RichTextBoxFinds.MatchCase)
                    End If
                Else
                    If WholeWord = True Then
                        Me.Find(FindText, RichTextBoxFinds.WholeWord)
                    Else
                        Me.Find(FindText)
                    End If
                End If

                If Not Me.SelectionLength = 0 Then
                    Me.SelectedText = ReplaceText
                Else
                    MsgBox("The following specified text was not found: " & FindText)
                End If


            Case True


                Dim i As Integer
                For i = 0 To Me.TextLength 'We know that strings we have to replace < TextLength of RichTextBox


                    If MatchCase = True Then
                        If WholeWord = True Then
                            Me.Find(FindText, RichTextBoxFinds.MatchCase Or RichTextBoxFinds.WholeWord)
                        Else
                            Me.Find(FindText, RichTextBoxFinds.MatchCase)
                        End If
                    Else
                        If WholeWord = True Then
                            Me.Find(FindText, RichTextBoxFinds.WholeWord)
                        Else
                            Me.Find(FindText)
                        End If
                    End If


                    If Not Me.SelectionLength = 0 Then
                        Me.SelectedText = ReplaceText
                    Else
                        MsgBox(i & " occurrence(s) replaced")
                        Exit For
                    End If
                Next i

        End Select
    End Sub
    
End Class

7. We can use RichTextBoxFinds enumerations (which enables us to specify how the search is performed when the Find method is called)such as MatchCase, WholeWord, for advance search by thinking logically.

8. When we use ReplaceAll = True, we do not know how many strings we have to replace, but we know that Strings we have to replace < textlength of the richtextbox. We use For Loop statement to implement it.

 

How To Use It

Now we have created our RichTextBoxFR control.

9. Select Build Menu --> Build Solution

10. Select RichTextBoxFR from the ToolBox  and draw it on the form.

11. Create a new form as above and name it frmFind. This form has

          i. Two TextBoxes :- FindTextBox and ReplaceTextBox

         ii. Three Buttons :- btnReplaceAll, btnReplace and btnCancel

        iii. Two CheckBoxes :- MatchCase, WholeWord

12. Here are the codes for the form

Public Class frmFind
 
      Private Sub btnReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceAll.Click
        If MatchCase.Checked Then

            If WholeWord.Checked Then
                frmMain.RichTextBoxFR1.FindAndReplace(FindTextBox.Text, ReplaceTextBox.Text, True, True, True)
            Else
                frmMain.RichTextBoxFR1.FindAndReplace(FindTextBox.Text, ReplaceTextBox.Text, True, True, False)
            End If

        Else ' If FindMe.MatchCase.Checked = False

            If WholeWord.Checked Then
                frmMain.RichTextBoxFR1.FindAndReplace(FindTextBox.Text, ReplaceTextBox.Text, True, False, True)
            Else
                frmMain.RichTextBoxFR1.FindAndReplace(FindTextBox.Text, ReplaceTextBox.Text, True, False, False)
            End If

        End If 'End FindMe.MatchCase.Checked 

    End Sub

    Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
        If MatchCase.Checked Then

            If WholeWord.Checked Then
                frmMain.RichTextBoxFR1.FindAndReplace(FindTextBox.Text, ReplaceTextBox.Text, False, True, True)
            Else
                frmMain.RichTextBoxFR1.FindAndReplace(FindTextBox.Text, ReplaceTextBox.Text, False, True, False)
            End If

        Else 'If FindMe.MatchCase.Checked = False

            If WholeWord.Checked Then
                frmMain.RichTextBoxFR1.FindAndReplace(FindTextBox.Text, ReplaceTextBox.Text, False, False, True)
            Else
                frmMain.RichTextBoxFR1.FindAndReplace(FindTextBox.Text, ReplaceTextBox.Text, False, False, False)
            End If

        End If 'End FindMe.MatchCase.Checked 

    End Sub
      
End Class

 

History

 created - 17 March 2006

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chatura Dilan


Member
I'm a student of University of Colombo School of Computing
I'm also a
Sun Certified Java Programmer
Occupation: Web Developer
Location: Sri Lanka Sri Lanka

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralThere are some bugs PinmemberJackie-leo21:26 9 Mar '09  
Generalbug occurred!! PinmemberKIM HyunJoong0:10 19 Mar '07  
GeneralRe: bug occurred!! PinmemberChatura Dilan21:46 10 Apr '07  
GeneralDemo Project PinmemberChatura Dilan1:09 19 Mar '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Mar 2006
Editor:
Copyright 2006 by Chatura Dilan
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project