SpellingError Class

Represents a misspelled word in an editing control (i.e. TextBox or RichTextBox).

System.Object
  System.Windows.Controls.SpellingError

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

public class SpellingError

The SpellingError type exposes the following members.

  NameDescription
Public propertySuggestionsGets a list of suggested spelling replacements for the misspelled word.
Top

  NameDescription
Public methodCorrectReplaces the spelling error text with the specified correction.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIgnoreAllInstructs the control to ignore this error and any duplicates for the remainder of the lifetime of the control.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

By default, when you enable spell checking in an editing control like TextBox or RichTextBox, you get spell-checking choices in the context menu. For example, when users right-click a misspelled word, they get a set of spelling suggestions or the option to Ignore All. However, when you override the default context menu with your own custom context menu, this functionality is lost, and you need to write code to reenable the spell-checking feature in the context menu. The following example shows how to enable this on a TextBox.

The following example shows the Extensible Application Markup Language (XAML) that creates a TextBox with some events that are used to implement the context menu.

<Page x:Class="SDKSample.SpellerCustomContextMenu"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded="OnWindowLoaded">

  <TextBox
    Name="myTextBox" 
    TextWrapping="Wrap"
    SpellCheck.IsEnabled="True"
    ContextMenuOpening="tb_ContextMenuOpening">
    In a custum menu you need to write code to add speler choices
    because everything in a custom context menu has to be added explicitly.
  </TextBox>

</Page>

The following example shows the code that implements the context menu.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SDKSample
{
    public partial class SpellerCustomContextMenu : Page
    {

        void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            //This is required for the first time ContextMenu invocation so that TextEditor doesnt handle it.
            myTextBox.ContextMenu = GetContextMenu();
        }
        void tb_ContextMenuOpening(object sender, RoutedEventArgs e)
        {
            int caretIndex, cmdIndex;
            SpellingError spellingError;

            myTextBox.ContextMenu = GetContextMenu();
            caretIndex = myTextBox.CaretIndex;

            cmdIndex = 0;
            spellingError = myTextBox.GetSpellingError(caretIndex);
            if (spellingError != null)
            {
                foreach (string str in spellingError.Suggestions)
                {
                    MenuItem mi = new MenuItem();
                    mi.Header = str;
                    mi.FontWeight = FontWeights.Bold;
                    mi.Command = EditingCommands.CorrectSpellingError;
                    mi.CommandParameter = str;
                    mi.CommandTarget = myTextBox;
                    myTextBox.ContextMenu.Items.Insert(cmdIndex, mi);
                    cmdIndex++;
                }
                Separator separatorMenuItem1 = new Separator();
                myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem1);
                cmdIndex++;
                MenuItem ignoreAllMI = new MenuItem();
                ignoreAllMI.Header = "Ignore All";
                ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
                ignoreAllMI.CommandTarget = myTextBox;
                myTextBox.ContextMenu.Items.Insert(cmdIndex, ignoreAllMI);
                cmdIndex++;
                Separator separatorMenuItem2 = new Separator();
                myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem2);
            }
        }

        // Gets a fresh context menu. 
        private ContextMenu GetContextMenu()
        {
            ContextMenu cm = new ContextMenu();

            //Can create STATIC custom menu items if exists here...
            MenuItem m1, m2, m3, m4;
            m1 = new MenuItem();
            m1.Header = "File";
            m2 = new MenuItem();
            m2.Header = "Save";
            m3 = new MenuItem();
            m3.Header = "SaveAs";
            m4 = new MenuItem();
            m4.Header = "Recent Files";

            //Can add functionality for the custom menu items here...

            cm.Items.Add(m1);
            cm.Items.Add(m2);
            cm.Items.Add(m3);
            cm.Items.Add(m4);

            return cm;
        }

    }
}

The code used for doing this with a RichTextBox is similar. The main difference is in the parameter passed to the GetSpellingError method. For a TextBox, pass the integer index of the caret position:

spellingError = myTextBox.GetSpellingError(caretIndex);

For a RichTextBox, pass the TextPointer that specifies the caret position:

spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft