Introduction
In this article, I am going to explain how to search and highlight text in Rich TextBox in a Windows Forms app using Visual Studio 2017.
STEP 1 - Start the Project
Let's create a new project using Visual Studio 2017.
In this article, I am going to explain how to search and highlight text in Rich TextBox in a Windows Forms app using Visual Studio 2017.
STEP 1 - Start the Project
Let's create a new project using Visual Studio 2017.
Select New Project--->Visual C#-->Windows Forms App (.NET Framework), give your project a name and click OK.

This action creates a WinForms project with a default form and you should see the Windows designer.
STEP 2 - Drag and Drop Control
Let's add a control to the form by dragging it from Toolbox and dropping it to the form. You will see a Rich TextBox 1 is added to the form. This control is now available to you in the code behind.
Search and Highlight Text
The textbox will be used to enter the search string and on the button click, we will be performing a search in the Rich TextBox. Add a class variable called 'start'. This variable will be the start position within the control text from where you need to begin searching.
Also, add another variable called 'IndexofsearchText' to hold position/index of the search string in the Rich TextBox. RTF stands for Rich Text Format, and this describes a certain file type where text formatting is encoded with special character embedded in the text. Programs such as Microsoft Word can use RTF documents.
Now, let's add another control to the form by dragging the other control from Toolbox to the form. You may also want to change the properties of the other controls.
Change the text of button and label control to what you like.
STEP 3 - Coding
Follow the coding given below,
STEP 3 - Coding
Follow the coding given below,


Tuan Nguyen AnhPosted Dec 6, 2020, 11:02 AM
At this line, startindex += wordstartIndex + word.Length; you should change to startindex = wordstartIndex + word.Length; because wordstartIndex + word.Length is already contains the next postion to lookup
LONE WOLFPosted Jun 8, 2019, 6:06 PM
Why when i execute it crashes ? Anyone can help ?
Yılmaz KaraPosted Feb 11, 2019, 12:25 AM
A very useful article, thank you. For those who want to see a different use, I found an article like this: http://www.ozcandegirmenci.com/searchable-richtextbox-control/ can review friends who are in need. In addition, you can access the project via github. Thanks Meneka.
Archit ShrivastavaPosted Aug 23, 2018, 7:12 AM
Nice article
maha lakshmiPosted Aug 14, 2018, 10:36 AM
Nice article
maha lakshmiPosted Aug 14, 2018, 10:35 AM
Good article
Guest UserPosted Aug 14, 2018, 10:31 AM
Good article