Custom Spell Checker Example for Blazor

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

Custom Spell Checker Example for Blazor 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 Blazor
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 Blazor Solution