Skip to main content

RichEditControl.ContentChanged Event

Occurs when the document content was changed.

Namespace: DevExpress.XtraRichEdit

Assembly: DevExpress.XtraRichEdit.v24.2.dll

Declaration

public event EventHandler ContentChanged

Event Data

The ContentChanged event's data class is EventArgs.

Remarks

This event occurs in the following situations:

  • a document is loaded into the RichEditControl using the LoadDocument method
  • a document is loaded using the Text, RtfText, HtmlText, MhtText, WordMLText properties
  • a new document is created
  • the document text is changed
  • document formatting is modified

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 and highlight it:

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

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

        // Obtain the search result
        if (searchResult.FindNext())
        {
            // Highlight entered word
            var characterProperties = richEditControl.Document.BeginUpdateCharacters(searchResult.CurrentResult);
            characterProperties.BackColor = System.Drawing.Color.Red;
            richEditControl.Document.EndUpdateCharacters(characterProperties);
        }
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ContentChanged event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also