Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/how-to-use-spell-checking-with-a-context-menu.md
T
Andy (Steve) De George be103da07e Replace various WPF include files with content (#1335)
* net-current-v30plus-md.md

* net-current-v40plus-md.md

* tla2sharptla-ui-md.md

* tla2sharptla-uiautomation-md.md

* tla2sharptla-winclient-md.md

* tla2sharptla-xaml-md.md

* tlasharptla-ui-md.md

* tlasharptla-uiautomation-md.md

* tlasharptla-winclient-md.md

* tlasharptla-xaml-md.md

* fix warnings
2022-03-16 12:14:11 -04:00

3.0 KiB

title, description, ms.date, ms.custom, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date ms.custom dev_langs helpviewer_keywords ms.assetid
How to: Use Spell Checking with a Context Menu Learn how to use Spell Checking with a Context Menu. 03/30/2017 devdivchpfy22
csharp
vb
enabling spell checking in a text box [WPF]
reenabling spell checking in a text box [WPF]
spell checking with a context menu [WPF]
61f69a20-2ff3-4056-9060-e32f4483ec5e

How to: Use Spell Checking with a Context Menu

By default, when you enable spell checking in an editing control like xref:System.Windows.Controls.TextBox or xref:System.Windows.Controls.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 xref:System.Windows.Controls.TextBox.

Define a Context Menu

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

[!code-xamlTextBoxMiscSnippets_snip#SpellerCustomContextMenuExampleWholePage]

Implement a Context Menu

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

[!code-csharpTextBoxMiscSnippets_snip#SpellerCustomContextMenuCodeExampleWholePage] [!code-vbTextBoxMiscSnippets_snip#SpellerCustomContextMenuCodeExampleWholePage]

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

spellingError = myTextBox.GetSpellingError(caretIndex);

For a xref:System.Windows.Controls.RichTextBox, pass the xref:System.Windows.Documents.TextPointer that specifies the caret position:

spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);

See also