Back to Devexpress

RichEditControl.AutoCorrect Event

windowsforms-devexpress-dot-xtrarichedit-dot-richeditcontrol-ed560499.md

latest4.9 KB
Original Source

RichEditControl.AutoCorrect Event

Fires when text is typed in the control.

Namespace : DevExpress.XtraRichEdit

Assembly : DevExpress.XtraRichEdit.v25.2.dll

NuGet Package : DevExpress.Win.RichEdit

Declaration

csharp
public event AutoCorrectEventHandler AutoCorrect
vb
Public Event AutoCorrect As AutoCorrectEventHandler

Event Data

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

PropertyDescription
AutoCorrectInfoGets 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:

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

Remarks

Handle the AutoCorrect event to analyze the input text and provide a specific object for replacement.

If you handle the AutoCorrect event, and do not intend to make any replacements, set e.AutoCorrectInfo to null instead of setting the AutoCorrectInfo.ReplaceWith to an empty string.

Note

We do not recommend calling methods used to access a document layout (e.g.,Document.CaretPosition or RichEditControl.GetBoundsFromPosition) in this event handler. This may result in an unhandled exception.

Example

The following code sample shows how to handle the RichEditControl.AutoCorrect event to replace the typed $ symbol with a picture of a dollar, and substitute the text enclosed in percent symbols with custom content.

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

    if (info.Text.Length <= 0)
        return;

    //Find a dollar and the percent sign in the obtained text
    for (; ; )
    {
        if (!info.DecrementStartPosition())
            return;

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

        if (info.Text[0] == '$')
        {
            //Replace the dollar sign with an image
            info.ReplaceWith = CreateImageFromResx("dollar_pic.png");
            e.AutoCorrectInfo = info;
            return;
        }

        if (info.Text[0] == '%')
        {
            //Replace a text between dollar signs with a custom content
            string replaceString = CalculateFunction(info.Text);
            if (!String.IsNullOrEmpty(replaceString))
            {
                info.ReplaceWith = replaceString;
                e.AutoCorrectInfo = info;
            }
            return;
        }
    }
}
vb
Private Sub richEditControl1_AutoCorrect(ByVal sender As Object, ByVal e As DevExpress.XtraRichEdit.AutoCorrectEventArgs) Handles richEditControl1.AutoCorrect
    Dim info As AutoCorrectInfo = e.AutoCorrectInfo
    e.AutoCorrectInfo = Nothing

    If info.Text.Length <= 0 Then
        Return
    End If

    ' Find a dollar and the percent sign in the obtained text
    Do
        If Not info.DecrementStartPosition() Then
            Return
        End If

        If IsSeparator(info.Text(0)) Then
            Return
        End If

        'Replace the dollar sign with an image
        If info.Text(0) = "$"c Then
            info.ReplaceWith = CreateImageFromResx("dollar_pic.png")
            e.AutoCorrectInfo = info
            Return
        End If

        'Replace a text between dollar signs with a custom content
        If info.Text(0) = "%"c Then
            Dim replaceString As String = CalculateFunction(info.Text)
            If Not String.IsNullOrEmpty(replaceString) Then
                info.ReplaceWith = replaceString
                e.AutoCorrectInfo = info
            End If
            Return
        End If
    Loop
End Sub

See Also

RichEditControl Class

RichEditControl Members

DevExpress.XtraRichEdit Namespace