A newer version of this page is available. Switch to the current version.

RichEditControl.ContentChanged Event

Occurs when the document content was changed.

Namespace: DevExpress.XtraRichEdit

Assembly: DevExpress.XtraRichEdit.v19.1.dll


public event EventHandler ContentChanged

Event Data

The ContentChanged event's data class is EventArgs.


This event occurs in the following situations:

Generally the ContentChanged event is fired several times when a document is modified. Since it depends on the complexity of the modification, you are advised against counting the number of times the event fires.

Note that the SubDocument.InsertText and SubDocument.InsertDocumentContent methods result in significant changes of the internal document model. The ContentChanged event occurs two times in this case, and you cannot predict when the second ContentChanged event fires.

Use the RichEditControl.Modified property in the event handler to distinguish between loading a new document and a situation when the document is modified. When a document is newly created, the Modified value is false. Otherwise, it is set to true.

The System.Windows.Forms.Control.TextChanged event is fired in the same situations as the ContentChanged event.

The code sample below shows how to handle the ContentChanged event to obtain currently entered word in the document

private void richEditControl1_ContentChanged(object sender, EventArgs e)
    //Get the caret position
    int caretPos = richEditControl1.Document.CaretPosition.ToInt();
    if ( caretPos == 0 )
        Text = string.Empty;

    string previousSymbol = richEditControl1.Document.GetText(richEditControl1.Document.CreateRange(caretPos - 1, 1));
    if ( Char.IsWhiteSpace(previousSymbol, 0) )
      // Search from right to left from the caret position to the start of the paragraph
        Paragraph currentParagraph = richEditControl1.Document.Paragraphs.Get(richEditControl1.Document.CaretPosition);
        DocumentRange searchRange = richEditControl1.Document.CreateRange(currentParagraph.Range.Start, caretPos - currentParagraph.Range.Start.ToInt());
        IRegexSearchResult searchResult = richEditControl1.Document.StartSearch(new Regex(@"\w+", RegexOptions.RightToLeft), searchRange);

        //Obtain the search result
        if ( searchResult.FindNext() )
            Text = richEditControl1.Document.GetText(searchResult.CurrentResult);
See Also