How to search text in RichEditControl
By FoxLearn 12/24/2024 3:54:54 AM 111
DevExpress provides a flexible way to handle text searching in the RichEditControl
via the StartSearch
method and the ISearchResult
interface, which offers a rich set of options to customize the search.
If you want to perform manual searches within a word file displayed in the RichEditControl
, you can use the StartSearch
method.
How to find a text in RichEditControl?
For example:
private void btnSearch_ItemClick(object sender, ItemClickEventArgs e) { string searchText = "Test"; // Define the search term ISearchResult searchResult = richEditControl1.Document.StartSearch(searchText); // Start searching if (searchResult != null) { // Loop through all found results while (searchResult.FindNext()) { // Begin updating the character properties for the current search result CharacterProperties cp = richEditControl1.Document.BeginUpdateCharacters(searchResult.CurrentResult); // Apply the formatting changes cp.Bold = true; cp.ForeColor = System.Drawing.Color.Blue; cp.BackColor = System.Drawing.Color.Yellow; cp.Underline = UnderlineType.ThickSingle; // End the update and apply the changes to the document richEditControl1.Document.EndUpdateCharacters(cp); } } else { // Handle the case where the search result is null XtraMessageBox.Show("No matches found for '" + searchText + "'.", "Message", MessageButton.OK, MessageIcon.Error); } }
How to scroll the first RichEditControl to the position of the searched text?
For example:
// Search, Navigate to text in richeditcontrol public void Search(string searchText) { // Start the search with whole word option and forward direction ISearchResult searchResult = richEditControl.Document.StartSearch(searchText, SearchOptions.WholeWord, SearchDirection.Forward); // Check if a match is found if (searchResult.FindNext()) { // Move the caret to the start of the found text richEditControl.Document.CaretPosition = searchResult.CurrentResult.Start; // Scroll to the caret position (0 is the margin in pixels) richEditControl.ScrollToCaret(0); } else { // No match found XtraMessageBox.Show("Text not found.", "Message", MessageButton.OK, MessageIcon.Error); } }
The StartSearch
method starts the search with the given text (searchValue
) and applies the specified search options. You can combine multiple search options using the bitwise OR operator (|
).
SearchOptions.WholeWord | SearchOptions.CaseSensitive
The FindNext()
method is called to find the next occurrence of the search term. If a match is found, it returns true
; otherwise, false
.
Once a match is found, the Selection
property of the RichEditControl.Document
is used to select the found text, making it easier to highlight it for the user.
// Move caret to the start of the found text richEditControl.Document.CaretPosition = searchResult.CurrentResult.Start; // Select the found text richEditControl.Document.Selection = searchResult.CurrentResult; // Scroll to the caret position (0 is the margin in pixels) richEditControl.ScrollToCaret(0);
After the caret is placed at the start of the found text, the ScrollToCaret(0)
method is called to ensure the found text is visible to the user. The 0
argument represents the top margin, in pixels, that should be applied when scrolling.
By using the StartSearch
, FindNext
, and SearchOptions
methods in the DevExpress.RichEditControl
API, you can easily implement a custom search feature for your Word files, offering full control over the search behavior.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#