AutoCorrect
- 5 minutes to read
The DXRichEdit AutoCorrect feature can help you automate inserting frequently used text, auto-correcting typing errors and misspelled capitalization. You can specify the built-in options construct a custom replacement table or handle the Autocorrect event to analyze the input text and provide the necessary substitution.
Built-in Automatic Correction Options
The following members enable you to enable certain automatic correction features:
Property | Description |
---|---|
DXRichEditAutoCorrectOptions.DetectUrls | Automatically detects an inserted web address, a network path or email address and converts it to a hyperlink. |
DXRichEditAutoCorrectOptions.CorrectTwoInitialCapitals | Corrects capitalization errors. |
DXRichEditAutoCorrectOptions.ReplaceTextAsYouType | Scans for entries as you type and replaces them with the designated text or image. |
DXRichEditAutoCorrectOptions.UseSpellCheckerSuggestions | Automatically detects and corrects typos and misspelled word if the RichEditControl.SpellChecker property specifies a spell checker component. If the spell checker provides one suggestion, this suggestion automatically substitutes the incorrect word. Otherwise, no changes are made. |
Access the built-in autocorrect options in XAML as shown in the code snippet below:
<dxre:RichEditControl x:Name="richEditControl1" DocumentSource="{Binding DataBaseDocumentSource}" CommandBarStyle="Ribbon">
<dxre:RichEditControl.AutoCorrectOptions>
<dxre:DXRichEditAutoCorrectOptions DetectUrls="False" UseSpellCheckerSuggestions="True" CorrectTwoInitialCapitals="True" />
</dxre:RichEditControl.AutoCorrectOptions>
</dxre:RichEditControl>
Automatic Correction Using Table Entries
The text expander functionality allows you to insert symbols, decipher abbreviations and insert images. Follow the steps below to implement the text expander:
Create a new AutoCorrectReplaceInfoCollection instance and fill it with AutoCorrectReplaceInfo entries:
private AutoCorrectReplaceInfoCollection LoadAbbrevs(string path) { AutoCorrectReplaceInfoCollection coll = new AutoCorrectReplaceInfoCollection(); string aLine = ""; AutoCorrectReplaceInfo acrInfoIm = new AutoCorrectReplaceInfo(":-)", CreateImageFromResx("smile.png")); coll.Add(acrInfoIm); if (File.Exists(path)) { StreamReader sr = new StreamReader(path); while (!(sr.EndOfStream)) { aLine = sr.ReadLine(); if (aLine != "START") continue; while (!(sr.EndOfStream)) { aLine = sr.ReadLine(); aLine = aLine.Trim(); string[] words = aLine.Split('='); if (words.Length == 2) { AutoCorrectReplaceInfo acrInfo = new AutoCorrectReplaceInfo(words[0], words[1]); coll.Add(acrInfo); } } } sr.Close(); } return coll; }
Call the IAutoCorrectService.SetReplaceTable method to register a created replacement list. Register the service after the RichEditControl instance is completely loaded, i.e. in the Window.Loaded event handler.
When you type a space, punctuation mark or carriage return (a character that typically means you have finished typing a word), AutoCorrect compares the word (or more precisely the group of characters) against its list of entries. If the word matches an entry, AutoCorrect substitutes this word with a replacement object.
In this example, the substitution is performed as shown in the animation below:
Automatic Correction Using the AutoCorrect Event
Handle the RichEditControl.AutoCorrect event to analyze the input text and provide a specific object for replacement. Use the following API (accessible using the AutoCorrectEventArgs.AutoCorrectInfo property) to perform the desired actions:
API | Description |
---|---|
AutoCorrectInfo.Text | Gets the input string to check whether it should be replaced. |
AutoCorrectInfo.IncrementStartPosition | Decreases a text range being analyzed by moving its start by one position. |
AutoCorrectInfo.DecrementStartPosition | Extends a text range being analyzed by moving its start by one position. |
AutoCorrectInfo.IncrementEndPosition | Extends a text range being analyzed by moving its end by one position. |
AutoCorrectInfo.DecrementEndPosition | Decreases a text range being analyzed by moving its end by one position. |
AutoCorrectInfo.ReplaceWith | Gets or sets the object used to replace the input string in the document. |
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;
}
}
}