Click here to Skip to main content
12,395,015 members (68,761 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked

Find And Replace Method For RichTextBox

, 18 Mar 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
An artcal about richtextbox control

Sample Image - findandriplace_rtb.jpg


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

<FONT color=#0000ff size=2>Public Class</FONT><FONT size=2> RichTextBoxFR</FONT>
<FONT color=#0000ff>    Inherits </FONT>RichTextBox

<FONT color=#0000ff>End Class</FONT>

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

<FONT color=#0000ff size=2>Public Class</FONT>

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

<FONT color=#0000ff size=2>Public Class</FONT>

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

<FONT color=#0000ff>Public Class</FONT>



 created - 17 March 2006


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


About the Author

Chatura Dilan
Web Developer
Sri Lanka Sri Lanka
I'm a student of University of Colombo School of Computing
I'm also a
Sun Certified Java Programmer

You may also be interested in...

Comments and Discussions

GeneralThere are some bugs Pin
Jackie-leo9-Mar-09 20:26
memberJackie-leo9-Mar-09 20:26 
Generalbug occurred!! Pin
KIM HyunJoong18-Mar-07 23:10
memberKIM HyunJoong18-Mar-07 23:10 
GeneralRe: bug occurred!! Pin
Chatura Dilan10-Apr-07 20:46
memberChatura Dilan10-Apr-07 20:46 
GeneralDemo Project Pin
Chatura Dilan19-Mar-06 0:09
memberChatura Dilan19-Mar-06 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 19 Mar 2006
Article Copyright 2006 by Chatura Dilan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid