$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
RichTextBox WinForms Control
Edit rich text content with text formatting options, mail-merge, table, and floating objects, etc.
Home > WinForms UI Controls > User Manual > Search in RichTextBox in C#

Search in RichTextBox for C# Windows Forms

KetticRichTextBox WinForms control allows the users to search contents in the document. This is achieved through using some methods for managing the selection of KetticRichTextBox. When the users combine the Search functionality and selection of document together, they can do a lot of operations to the text of document. By the search functionality, we can highlight text of document, as well as replace words and collocations, etc.

C# Search Text in KetticRichTextBox

The following C# code sample demonstrates how to search a string in the text of document in KetticRichTextBox.

private void SelectAllMatches(string toSearch)
{
this.ketticRichTextBox1.Document.Selection.Clear();
DocumentTextSearch search = new DocumentTextSearch(this.ketticRichTextBox1.Document);
foreach (var textRange in search.FindAll(toSearch))
{
this.ketticRichTextBox1.Document.Selection.AddSelectionStart(textRange.StartPosition);
this.ketticRichTextBox1.Document.Selection.AddSelectionEnd(textRange.EndPosition);
}
}
The search functionality allows the users to apply all regular expressions and escape the search string by invoking the C# code below.

toSearch = Regex.Escape(toSearch);
In addition, the KetticRichTextBox control allows the users to select text from the search results. This functionality will be performed on the parts of the document that are selected currently. The C# code below is an example shows how to customize the selection.

this.ketticRichTextBox1.ChangeTextHighlightColor(Color.LightGray);
this.ketticRichTextBox1.ChangeFontSize(Unit.PointToDip(32));
this.ketticRichTextBox1.ChangeFontFamily("Courier New");
When the users need to remove the selection, they can call the Clear() method of the DocumentSelection object as the simple C# code snippet as below.

this.ketticRichTextBox1.Document.Selection.Clear();
Moreover, KetticRichTextBox control allows the users to extend the search and selection functionality to replace the text of searched results. The following C# code demonstrates how to replace the searched string.
When we add or remove strings to the document, the positions will be changed, so that we may need to create new TextRanges with document positions to tack the changes. The users can enable the second property of the two properties constructor to keep the relative position when the document changes.
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls