This documentation is archived and is not being maintained.

RichTextBox.GetSpellingError Method

Returns a SpellingError object associated with any spelling error at a specified position in the contents of the RichTextBox.

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

'Declaration
Public Function GetSpellingError ( _
	position As TextPointer _
) As SpellingError

Parameters

position
Type: System.Windows.Documents.TextPointer
A TextPointer that specifies a position and logical direction that resolves to a character to examine for a spelling error. Use the LogicalDirection property of this TextPointer to specify the direction of the character to examine.

Return Value

Type: System.Windows.Controls.SpellingError
A SpellingError object containing the details of the spelling error found at the character indicated by position, or Nothing if no spelling error exists at the specified character.

A TextPointer like position usually specifies a position between two characters. Use the LogicalDirection property of position to specify which character to examine.

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.



Namespace SDKSample
    Partial Public Class SpellerCustomContextMenu
        Inherits Page

        Private Sub OnWindowLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)

            'This is required for the first time ContextMenu invocation 
            'so that TextEditor doesnt handle it.
            myTextBox.ContextMenu = GetContextMenu()
        End Sub

        Private Sub tb_ContextMenuOpening(ByVal sender As Object,
                                          ByVal e As RoutedEventArgs)

            Dim caretIndex, cmdIndex As Integer
            Dim spellingError As SpellingError

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

            cmdIndex = 0
            spellingError = myTextBox.GetSpellingError(caretIndex)
            If spellingError IsNot Nothing Then
                For Each str As String In spellingError.Suggestions
                    Dim mi As 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 += 1
                Next str
                Dim separatorMenuItem1 As New Separator()
                myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem1)
                cmdIndex += 1
                Dim ignoreAllMI As New MenuItem()
                ignoreAllMI.Header = "Ignore All"
                ignoreAllMI.Command = EditingCommands.IgnoreSpellingError
                ignoreAllMI.CommandTarget = myTextBox
                myTextBox.ContextMenu.Items.Insert(cmdIndex, ignoreAllMI)
                cmdIndex += 1
                Dim separatorMenuItem2 As New Separator()
                myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem2)
            End If
        End Sub

        ' Gets a fresh context menu. 
        Private Function GetContextMenu() As ContextMenu
            Dim cm As New ContextMenu()

            'Can create STATIC custom menu items if exists here...
            Dim m1, m2, m3, m4 As MenuItem
            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
        End Function

    End Class
End Namespace


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, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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