How to search text in RichEditControl

By Tan Lee Published on Dec 24, 2024  279
To search for text in a RichEditControl in DevExpress, you can use the RichEditControl.Document.StartSearch method.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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 (|).

1
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.

1
2
3
4
5
6
7
8
 // 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.