Custom Spell Checker Example for WPF

Nevron NOV RichTextEditor for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF RichTextEditor examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Custom Spell Checker Example for WPF Description

The example demonstrates how to implement custom spell check functionality. In this example the custom spellchecker searched for the word "Nevron" and when it finds it will underline it. This functionality also works when you type text in the editor.


Custom Spell Checker Example

C# Custom Spell Checker Example Code for Nevron RichTextEditor for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.Text.SpellCheck;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NCustomSpellCheckerExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NStackPanel stack = new NStackPanel();
			stack.FillMode = ENStackFillMode.Last;
			stack.FitMode = ENStackFitMode.Last;
			stack.VerticalSpacing = 10;

			m_RichTextView = new NRichTextView();
			m_RichTextView.Content.Sections.Clear();

			NSection section = new NSection();
			m_RichTextView.Content.Sections.Add(section);
			stack.Add(m_RichTextView);

            section.Blocks.Add(new NParagraph(Text1));
			section.Blocks.Add(new NParagraph(Text2));

			m_RichTextView.SpellChecker = new CustomSpellchecker();

			// the following code shows how to disable the mini toolbar and leave only the text proofing context menu builder
			m_RichTextView.ContextMenuBuilder.Groups.Clear();
			m_RichTextView.ContextMenuBuilder.ShowMiniToolbar = false;
			m_RichTextView.ContextMenuBuilder.Groups.Add(new Nevron.Nov.Text.UI.NProofingMenuGroup());

			return stack;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			
			NToggleButton spellCheckButton = new NToggleButton("Enable Spell Checking");
			spellCheckButton.CheckedChanged += OnSpellCheckButtonCheckedChanged;
			stack.Add(spellCheckButton);

			spellCheckButton.Checked = true;

			return stack;
		}
		public string GetExampleDescription()
		{
			return @"

The example demonstrates how to implement custom spell check functionality. In this example the custom spellchecker searched for the word ""Nevron"" and when it finds it will underline it. This functionality also works when you type text in the editor.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Rich Text Spell Check", "https://www.nevron.com/docs/openvision/rich-text-spell-check.html"), ("Rich Text Documents - Overview", "https://www.nevron.com/docs/openvision/rich-text-documents-overview.html"), ("Rich Text Document Validation", "https://www.nevron.com/docs/openvision/rich-text-document-validation.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnSpellCheckButtonCheckedChanged(NValueChangeEventArgs arg) { m_RichTextView.SpellChecker.Enabled = (bool)arg.NewValue; } #endregion #region Fields private NRichTextView m_RichTextView; #endregion #region Static Methods #endregion #region Constants private const string Text1 = "Nevron Softuare is a globl leader in component based data vizualization technology " + "for a divrese range of Microsoft centric platforms. Built with perfectin, usability and enterprize level featurs " + "in mind, our components deliverr advanced digital dachboards and diagrams that are not to be matched."; private const string Text2 = "Tuday Nevron components are used by many Fortune 500 companis and thousands of developers " + "and IT profesionals worldwide."; private const long BytesInMB = 1024 * 1024; #endregion #region Nested Types /// /// Sample class that shows how to implement a custom spell checker /// private class CustomSpellchecker : NSpellChecker { /// /// Default constructor (mandatory for all NOV DOM objects) /// public CustomSpellchecker() { } /// /// Static constructor (mandatory for all NOV DOM objects) /// static CustomSpellchecker() { CustomSpellcheckerSchema = NSchema.Create(typeof(CustomSpellchecker), NSpellCheckerSchema); } /// /// Gets a list of misspelled word ranges /// /// /// when set to true the spellchecker must check the words that intersect the specified range as the user may be currently typing there. /// the protected word range /// public override NList GetMisspelledWordRanges(char[] chars, bool protectWordRange, NRangeI protectedWordRange) { string text = new string(chars); string invalidWord = "Nevron"; int index = 0; NList misspelledWordRanges = new NList(); while ((index = text.IndexOf(invalidWord, index)) != -1) { NRangeI currentRange = new NRangeI(index, index + invalidWord.Length - 1); // skip the currently protected spellcheck word, because the user is currently typing in there if (!protectWordRange || !protectedWordRange.Equals(currentRange)) { misspelledWordRanges.Add(currentRange); } index += invalidWord.Length; } return misspelledWordRanges; } public override IEnumerator GetSuggestions(char[] chars, int beginIndex, int endIndex) { return base.GetSuggestions(chars, beginIndex, endIndex); } private static readonly NSchema CustomSpellcheckerSchema; } } #endregion } }

Download a fully functional C# (.cs) Custom Spell Checker Example for WPF Solution

VB.NET Custom Spell Checker Example Code for Nevron RichTextEditor for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.DataStructures
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Text
Imports Nevron.Nov.Text.SpellCheck
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System.Collections.Generic

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NCustomSpellCheckerExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            Dim stack As NStackPanel = New NStackPanel()
            stack.FillMode = ENStackFillMode.Last
            stack.FitMode = ENStackFitMode.Last
            stack.VerticalSpacing = 10

            m_RichTextView = New NRichTextView()
            m_RichTextView.Content.Sections.Clear()

            Dim section As NSection = New NSection()
            m_RichTextView.Content.Sections.Add(section)
            stack.Add(m_RichTextView)

            section.Blocks.Add(New NParagraph(Text1))
            section.Blocks.Add(New NParagraph(Text2))

            m_RichTextView.SpellChecker = New CustomSpellchecker()

            ' the following code shows how to disable the mini toolbar and leave only the text proofing context menu builder
            m_RichTextView.ContextMenuBuilder.Groups.Clear()
            m_RichTextView.ContextMenuBuilder.ShowMiniToolbar = False
            m_RichTextView.ContextMenuBuilder.Groups.Add(New Nov.Text.UI.NProofingMenuGroup())

            Return stack
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()

            Dim spellCheckButton As NToggleButton = New NToggleButton("Enable Spell Checking")
            spellCheckButton.CheckedChanged += AddressOf OnSpellCheckButtonCheckedChanged
            stack.Add(spellCheckButton)

            spellCheckButton.Checked = True

            Return stack
        End Function
        Public Function GetExampleDescription() As String
            Return "

The example demonstrates how to implement custom spell check functionality. In this example the custom spellchecker searched for the word ""Nevron"" and when it finds it will underline it. This functionality also works when you type text in the editor.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Rich Text Spell Check", "https://www.nevron.com/docs/openvision/rich-text-spell-check.html"), ("Rich Text Documents - Overview", "https://www.nevron.com/docs/openvision/rich-text-documents-overview.html"), ("Rich Text Document Validation", "https://www.nevron.com/docs/openvision/rich-text-document-validation.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnSpellCheckButtonCheckedChanged(arg As NValueChangeEventArgs) m_RichTextView.SpellChecker.Enabled = CBool(arg.NewValue) End Sub #End Region #Region "Fields" Private m_RichTextView As NRichTextView #End Region #Region "Static Methods" #End Region #Region "Constants" Private Const Text1 As String = "Nevron Softuare is a globl leader in component based data vizualization technology " & "for a divrese range of Microsoft centric platforms. Built with perfectin, usability and enterprize level featurs " & "in mind, our components deliverr advanced digital dachboards and diagrams that are not to be matched." Private Const Text2 As String = "Tuday Nevron components are used by many Fortune 500 companis and thousands of developers " & "and IT profesionals worldwide." Private Const BytesInMB As Long = 1024 * 1024 #End Region #Region "Nested Types" ''' ''' Sample class that shows how to implement a custom spell checker ''' Private Class CustomSpellchecker Inherits NSpellChecker ''' ''' Default constructor (mandatory for all NOV DOM objects) ''' Public Sub New() End Sub ''' ''' Static constructor (mandatory for all NOV DOM objects) ''' Shared Sub New() CustomSpellcheckerSchema = NSchema.Create(GetType(CustomSpellchecker), NSpellCheckerSchema) End Sub ''' ''' Gets a list of misspelled word ranges ''' ''' ''' when set to true the spellchecker must check the words that intersect the specified range as the user may be currently typing there. ''' the protected word range ''' Public Overrides Function GetMisspelledWordRanges(chars As Char(), protectWordRange As Boolean, protectedWordRange As NRangeI) As NList(Of NRangeI) Dim text As String = New String(chars) Dim invalidWord = "Nevron" Dim index = 0 Dim misspelledWordRanges As NList(Of NRangeI) = New NList(Of NRangeI)() While CSharpImpl.__Assign(index, text.IndexOf(invalidWord, index)) <> -1 Dim currentRange As NRangeI = New NRangeI(index, index + invalidWord.Length - 1) ' skip the currently protected spellcheck word, because the user is currently typing in there If Not protectWordRange OrElse Not protectedWordRange.Equals(currentRange) Then misspelledWordRanges.Add(currentRange) End If index += invalidWord.Length End While Return misspelledWordRanges End Function Public Overrides Function GetSuggestions(chars As Char(), beginIndex As Integer, endIndex As Integer) As IEnumerator(Of Char()) Return MyBase.GetSuggestions(chars, beginIndex, endIndex) End Function Private Shared ReadOnly CustomSpellcheckerSchema As NSchema Private Class CSharpImpl Shared Function __Assign(Of T)(ByRef target As T, value As T) As T target = value Return value End Function End Class End Class End Class #End Region End Class

Download a fully functional VB.NET (.vb) Custom Spell Checker Example for WPF Solution