Skip to main content

DevExpress v24.1 Update — Your Feedback Matters

Our What's New in v24.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

RichEditControl.AutoCorrect Event

Fires when text is typed in the control.

Namespace: DevExpress.Xpf.RichEdit

Assembly: DevExpress.Xpf.RichEdit.v24.1.dll

NuGet Package: DevExpress.Wpf.RichEdit

#Declaration

public event AutoCorrectEventHandler AutoCorrect

#Event Data

The AutoCorrect event's data class is AutoCorrectEventArgs. The following properties provide information specific to this event:

Property Description
AutoCorrectInfo Gets or sets the AutoCorrectInfo object that provides methods to analyze input text and contains a replacement object.

The event data class exposes the following methods:

Method Description
CreateAutoCorrectInfo() Allows you to create a new AutoCorrectInfo instance for use within the event handler.

#Remarks

Handle the AutoCorrect event to implement complex replacements as you type.

The RichEditControl.AutoCorrect event is handled to replace the typed $ symbol with a picture of a dollar, and substitute the text enclosed in percent symbols with custom content.

To accomplish this task, a document text obtained via the AutoCorrectEventArgs.AutoCorrectInfo property is amended by expanding its left boundary using the AutoCorrectInfo.DecrementStartPosition method, until a special symbol or a word separator symbol is found. If a resulting string meets certain conditions, such as whether it is a dollar sign or a %date% string, a replacement object is passed to the document via the AutoCorrectInfo.ReplaceWith property. A replacement object can be a string or an image.

private void richEditControl1_AutoCorrect(object sender, DevExpress.XtraRichEdit.AutoCorrectEventArgs e)
{
    AutoCorrectInfo info = e.AutoCorrectInfo;
    e.AutoCorrectInfo = null;

    if (info.Text.Length <= 0)
        return;
    for (; ; ) {
        if (!info.DecrementStartPosition())
            return;

        if (IsSeparator(info.Text[0]))
            return;

        if (info.Text[0] == '$') {
            info.ReplaceWith = CreateImageFromResx("dollar_pic.png");
            e.AutoCorrectInfo = info;
            return;
        }

        if (info.Text[0] == '%') {
            string replaceString = CalculateFunction(info.Text);
            if (!String.IsNullOrEmpty(replaceString)) {
                info.ReplaceWith = replaceString;
                e.AutoCorrectInfo = info;
            }
            return;
        }
    }
}
See Also