Back to Devexpress

ISearchResult Interface

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-5c812414.md

latest6.5 KB
Original Source

ISearchResult Interface

Defines the interface used for text searching.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
[ComVisible(true)]
public interface ISearchResult
vb
<ComVisible(True)>
Public Interface ISearchResult

The following members return ISearchResult objects:

Remarks

The SubDocument.StartSearch method provides the ISearchResult interface. You can then start searching, or make replacements.

The following code demonstrates how to create the ISearchResult interface, and use it to perform a search. The search interface is created for a case sensitive search, matching only whole words, i.e. strings delimited with characters which are not alphabetical or decimal digits.

The SearchStringIsEmpty() function is a custom function that checks the search text and returns true if the search string is empty.

If the search direction is SearchDirection.Forward, the search starts from the end of the current selection and proceeds forward to the end of the document. The ISearchResult.FindNext method is called to perform a search. When a matching text is found, it is selected and the document is scrolled to make the selection visible. The range containing the matched string can be obtained via the ISearchResult.CurrentResult property.

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
        ISearchResult searchResult;
        Document Document { get { return this.richEditControl1.Document; } }
        SearchDirection direction = SearchDirection.Forward;
        SearchOptions options = SearchOptions.WholeWord |
            SearchOptions.CaseSensitive;

        ISearchResult SearchResult {
            get {
                if (searchResult == null)
                    searchResult = CreateSearchResult();
                return searchResult;
            }
        }
        private ISearchResult CreateSearchResult() {
            DocumentRange range;
            if (direction == SearchDirection.Forward) {
                int startPos = Document.Selection.End.ToInt();
                int length = Document.Range.End.ToInt() - startPos;
                range = Document.CreateRange(startPos, length);
            }
            else {
                int length = Document.Selection.Start.ToInt();
                range = Document.CreateRange(0, length);
            }
            return Document.StartSearch(searchString, options, direction, range);
        }
        private bool FindNext() {
            if(SearchStringIsEmpty())
                return false;
            if(SearchResult.FindNext()) {
                Document.Selection = SearchResult.CurrentResult;
                this.richEditControl1.ScrollToCaret();
                return true;
            }
            else {
                XtraMessageBox.Show("Search is complete.", Application.ProductName);
                return false;
            }
        }
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
        Private searchResult_Renamed As ISearchResult
        Private ReadOnly Property Document() As Document
            Get
                Return Me.richEditControl1.Document
            End Get
        End Property
        Private direction As SearchDirection = SearchDirection.Forward
        Private options As SearchOptions = SearchOptions.WholeWord Or _
          SearchOptions.CaseSensitive

        Private ReadOnly Property SearchResult() As ISearchResult
            Get
                If searchResult_Renamed Is Nothing Then
                    searchResult_Renamed = CreateSearchResult()
                End If
                Return searchResult_Renamed
            End Get
        End Property
        Private Function CreateSearchResult() As ISearchResult
            Dim range As DocumentRange
            If direction = SearchDirection.Forward Then
                Dim startPos As Integer = Document.Selection.End.ToInt()
                Dim length As Integer = Document.Range.End.ToInt() - startPos
                range = Document.CreateRange(startPos, length)
            Else
                Dim length As Integer = Document.Selection.Start.ToInt()
                range = Document.CreateRange(0, length)
            End If
            Return Document.StartSearch(searchString, options, direction, range)
        End Function
        Private Function FindNext() As Boolean
            If SearchStringIsEmpty() Then
                Return False
            End If
            If SearchResult.FindNext() Then
                Document.Selection = SearchResult.CurrentResult
                Me.richEditControl1.ScrollToCaret()
                Return True
            Else
                XtraMessageBox.Show("Search is complete.", Application.ProductName)
                Return False
            End If
        End Function

See Also

ISearchResult Members

StartSearch

DevExpress.XtraRichEdit.API.Native Namespace