windowsforms-devexpress-dot-xtrarichedit-dot-richeditcontrol-0065f6cf.md
Occurs when the document content was changed.
Namespace : DevExpress.XtraRichEdit
Assembly : DevExpress.XtraRichEdit.v25.2.dll
NuGet Package : DevExpress.Win.RichEdit
public event EventHandler ContentChanged
Public Event ContentChanged As EventHandler
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 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);
}
}
}
Private Sub richEditControl1_ContentChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim caretPos As Integer = richEditControl1.Document.CaretPosition.ToInt()
'Get the caret position
If caretPos = 0 Then
Text = String.Empty
Return
End If
Dim previousSymbol As String = richEditControl1.Document.GetText(richEditControl1.Document.CreateRange(caretPos - 1, 1))
If Not String.IsNullOrEmpty(previousSymbol) AndAlso Char.IsWhiteSpace(previousSymbol, 0) Then
Dim currentParagraph As Paragraph = richEditControl.Document.Paragraphs.[Get](richEditControl.Document.CaretPosition)
Dim searchRange As DocumentRange = richEditControl.Document.CreateRange(currentParagraph.Range.Start, caretPos - currentParagraph.Range.Start.ToInt())
Dim searchResult As IRegexSearchResult = richEditControl.Document.StartSearch(New Regex("\w+", RegexOptions.RightToLeft), searchRange)
If searchResult.FindNext() Then
Dim characterProperties = richEditControl.Document.BeginUpdateCharacters(searchResult.CurrentResult)
characterProperties.BackColor = System.Drawing.Color.Red
richEditControl.Document.EndUpdateCharacters(characterProperties)
End If
End If
End Sub
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.
winforms-rich-text-editor-retain-original-image-uri-in-html-document/CS/Form1.cs#L21
embedImagesCheck.EditValue = true;
richEditControl1.ContentChanged += new EventHandler(richEditControl1_ContentChanged);
}
winforms-rich-text-editor-retain-original-image-uri-in-html-document/VB/Form1.vb#L17
embedImagesCheck.EditValue = True
AddHandler richEditControl1.ContentChanged, AddressOf richEditControl1_ContentChanged
End Sub
See Also