Skip to main content

ISpellCheckService.CheckWordSpelling(String) Method

Returns whether the specified word is spelled correctly.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.v24.1.dll

NuGet Package: DevExpress.Blazor

Declaration

bool CheckWordSpelling(
    string word
)

Parameters

Name Type Description
word String

A word.

Returns

Type Description
Boolean

true if the word is spelled correctly; otherwise, false.

Remarks

The Rich Text Editor passes words from the active sub-document to the CheckWordSpelling method to check their spelling. When this method returns false, the component considers the word misspelled and underlines it with a wavy red line.

A user can right-click a misspelled word to invoke the context menu. The context menu displays suggested replacements for this word. To generate suggestions, the Rich Text Editor passes the misspelled word to the GetSpellingSuggestions(String) method.

Check spelling in the Rich Text Editor

The following code snippet implements an ISpellCheckService interface:

using DevExpress.Blazor.RichEdit;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

public class MySpellCheckService : ISpellCheckService {
    public CultureInfo Culture { get; set; }
    public bool AddToDictionaryEnabled { get; }
    public int MaxSuggestionCount { get; }
    string PathToDictionary { get;}
    List<string> CorrectWords { get;}

    public MySpellCheckService(string path) {
        PathToDictionary = path;
        Culture = CultureInfo.InvariantCulture;
        AddToDictionaryEnabled = true;
        MaxSuggestionCount = 7;
        CorrectWords = File.ReadLines(PathToDictionary).ToList();
        CorrectWords.Sort();
    }

    public void AddToDictionary(string word) {
        if (Culture.TwoLetterISOLanguageName == "en") {
            word = word.ToLower();
            int placeToInsert = CorrectWords.BinarySearch(word);
            CorrectWords.Insert(placeToInsert, word);
            File.AppendAllText(PathToDictionary, word + '\n');
        }
    }

    public bool CheckWordSpelling(string word) {
        if (Culture.TwoLetterISOLanguageName == "en") {
            int index = CorrectWords.BinarySearch(word.ToLower());
            return (index >= 0);
        }
        return false;
    } 

    public string[] GetSpellingSuggestions(string word) {
        List<string> suggestions = new List<string>();
        // Generate suggestions and add them to the list
        return suggestions.ToArray();
    }
}

Refer to the following topic for more information about the built-in spell check service: Spell Check.

See Also