Search and Highlight Text in a RichTextBox
The RichTextBox is a Windows Form control that provides the user with the functionality to enter, edit and perform some advanced formatting features on the text. Besides its other uses, the RichTextBox also allows us to search and highlight text, which is the focus of this article. In this article, we will attempt to replicate the Find and FindNext functions found in text editors.
The Find() method of the RichTextBox comes with 7 overloaded methods. You can find the description for each one of them over here using the MSDN documentation. In this article, we will be using the last one of the overloads, that is the Find(String, Int32, Int32, RichTextBoxFinds).
Find(String, Int32, Int32, RichTextBoxFinds) - Searches the text in a RichTextBox control for a string within a range of text within the control and with specific options applied to the search.
The Find method of the RichTextBox control searches for text specified in the string parameter. If the text is found, it returns the first character position of the search string in the RichTextBox. If the text is not found, the Find() returns -1. If you specify the same value for the start position and the end position, the Find method searches the entire text, starting from the zero position.
Let us see some code:
Step 1: Create a new Windows Forms application. Drag and drop a RichTextBox(rtb) control, a textbox(txtSearch) and a button(btnFind) control to the form. The textbox will be used to enter the search string and on the button click, we will be performing a search in the RichTextBox. Add a class level variable called ‘start’. This variable will be the start position within the control's text from where you need to begin searching. Also add another variable called ‘indexOfSearchText’ to hold the position/index of the search string in the RichTextBox.
int start = 0;
int indexOfSearchText = 0;
Dim start AsInteger = 0
Dim indexOfSearchText AsInteger = 0
Step 2: In the button click event, add the following code. The FindMyText() function, which we will visit in Step 3, is called by passing the search string, the start and the end index. If the string is found in the RichTextBox, the position of the search string is returned and highlighted. The function has been marked with appropriate comments to help you understand the code.
privatevoid btnFind_Click(object sender, EventArgs e)
int startindex = 0;
if(txtSearch.Text.Length > 0)
startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length);
// If string was found in the RichTextBox, highlight it
if (startindex >= 0)
// Set the highlight color as red
rtb.SelectionColor = Color.Red;
// Find the end index. End Index = number of characters in textbox
int endindex = txtSearch.Text.Length;
// Highlight the search string
// mark the start position after the position of
// last search string
start = startindex + endindex;
PrivateSub btnFind_Click(ByVal sender AsObject, ByVal e As EventArgs)
Dim startindex AsInteger = 0
If txtSearch.Text.Length > 0 Then
startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length)
' If string was found in the RichTextBox, highlight it
If startindex >= 0 Then
' Set the highlight color as red
rtb.SelectionColor = Color.Red
' Find the end index. End Index = number of characters in textbox
Dim endindex AsInteger = txtSearch.Text.Length
' Highlight the search string
' mark the start position after the position of
' last search string
start = startindex + endindex
Step 3: Now let us add the functionality to the FindNext() function. The function accepts the search string, and the start and end index. The search happens within the index specified. The validation occurs first where the start and end index are checked for valid values. We then use the Find() function of the RichTextBox to find the position of the search string in the RichtTextBox. If the string is found, the position of the string within the RichTextBox is returned. If the string is not found, -1 is returned. The function has been marked with appropriate comments to help you understand the code.
publicint FindMyText(string txtToSearch, int searchStart, int searchEnd)
// Unselect the previously searched string
if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
// Set the return value to -1 by default.
int retVal = -1;
// A valid starting index should be specified.
// if indexOfSearchText = -1, the end of search
if (searchStart >= 0 && indexOfSearchText >=0)
// A valid ending index
if (searchEnd > searchStart || searchEnd == -1)
// Find the position of search string in RichTextBox
indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
// Determine whether the text was found in richTextBox1.
if (indexOfSearchText != -1)
// Return the index to the specified search text.
retVal = indexOfSearchText;
PublicFunction FindMyText(ByVal txtToSearch AsString, ByVal searchStart AsInteger, ByVal searchEnd AsInteger) AsInteger
' Unselect the previously searched string
If searchStart > 0 AndAlso searchEnd > 0 AndAlso indexOfSearchText >= 0 Then
' Set the return value to -1 by default.
Dim retVal AsInteger = -1
' A valid starting index should be specified.
' if indexOfSearchText = -1, the end of search
If searchStart >= 0 AndAlso indexOfSearchText >=0 Then
' A valid ending index
If searchEnd > searchStart OrElse searchEnd = -1 Then
' Find the position of search string in RichTextBox
indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None)
' Determine whether the text was found in richTextBox1.
If indexOfSearchText <> -1 Then
' Return the index to the specified search text.
retVal = indexOfSearchText
Step 4: If a user changes the search string in the textbox, you can handle the TextChanged event to reset the value of ‘start’ and ‘indexOfSearchText’ to zero. This starts the new search from the beginning.
// Reset the richtextbox when user changes the search string
privatevoid textBox1_TextChanged(object sender, EventArgs e)
start = 0;
indexOfSearchText = 0;
' Reset the richtextbox when user changes the search string
PrivateSub textBox1_TextChanged(ByVal sender AsObject, ByVal e As EventArgs)
start = 0
indexOfSearchText = 0
That’s it. Run the code and see your implementation of search and highlight in the RichTextBox. I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!