Spell Check 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.

Spell Check Example for WPF Description

The example demonstrates the built in spell checker functionality.


Spell Check Example

C# Spell Check 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.UI;
using Nevron.Nov.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

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

		public Form1()
		{
			InitializeComponent();

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

			// 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));

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

			NButton suggetsionsButton = new NButton("Get Suggestions");
			suggetsionsButton.Click += OnSuggetsionsButtonClick;
			stack.Add(suggetsionsButton);

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

The example demonstrates the built in spell checker functionality.

"; } /// /// 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"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnSpellCheckButtonCheckedChanged(NValueChangeEventArgs arg) { m_RichTextView.SpellChecker.Enabled = (bool)arg.NewValue; } private void OnSuggetsionsButtonClick(NEventArgs args) { NList paragraphs = m_RichTextView.Selection.GetSelectedParagraphs(); if (paragraphs.Count == 0) return; NParagraph paragraph = paragraphs[0]; // Determine the current word NList words = paragraph.GetWordRanges(); int index = m_RichTextView.EditingRoot.Selection.Position.InsertIndex - paragraph.Range.Begin; bool hasWord = false; NRangeI wordRange = NRangeI.Zero; for (int i = 0; i < words.Count; i++) { if (words[i].Contains(index)) { hasWord = true; wordRange = words[i]; } } if (!hasWord) { NMessageBox.Show(m_RichTextView.DisplayWindow, "You should click in a word first", "Warning", ENMessageBoxButtons.OK, ENMessageBoxIcon.Warning); return; } char[] word = paragraph.Text.Substring(wordRange.Begin, wordRange.GetLength() + 1).ToCharArray(); string title = "Suggestions for '" + new string(word) + "'"; string content; if (m_RichTextView.SpellChecker.IsCorrect(word, 0, word.Length - 1) == false) { IEnumerator suggestions = m_RichTextView.SpellChecker.GetSuggestions(word, 0, word.Length - 1); StringBuilder sb = new StringBuilder(); while (suggestions.MoveNext()) { if (sb.Length > 0) { sb.Append("\n"); } sb.Append(suggestions.Current); } content = sb.ToString(); } else { content = "The word is correct."; } NMessageBox.Show(m_RichTextView.DisplayWindow, content, title, ENMessageBoxButtons.OK); } #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 #endregion } }

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

VB.NET Spell Check 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.UI
Imports Nevron.Nov.Windows.Wpf
Imports System.Collections.Generic
Imports System.Text

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 - NSpellCheckExample"

		' 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))

            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)

            Dim suggetsionsButton As NButton = New NButton("Get Suggestions")
            suggetsionsButton.Click += AddressOf OnSuggetsionsButtonClick
            stack.Add(suggetsionsButton)

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

The example demonstrates the built in spell checker functionality.

" 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")} 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 Private Sub OnSuggetsionsButtonClick(args As NEventArgs) Dim paragraphs As NList(Of NParagraph) = m_RichTextView.Selection.GetSelectedParagraphs() If paragraphs.Count = 0 Then Return Dim paragraph = paragraphs(0) ' Determine the current word Dim words As NList(Of NRangeI) = paragraph.GetWordRanges() Dim index As Integer = m_RichTextView.EditingRoot.Selection.Position.InsertIndex - paragraph.Range.Begin Dim hasWord = False Dim wordRange = NRangeI.Zero For i = 0 To words.Count - 1 If words(i).Contains(index) Then hasWord = True wordRange = words(i) End If Next If Not hasWord Then NMessageBox.Show(m_RichTextView.DisplayWindow, "You should click in a word first", "Warning", ENMessageBoxButtons.OK, ENMessageBoxIcon.Warning) Return End If Dim word As Char() = paragraph.Text.Substring(wordRange.Begin, wordRange.GetLength() + 1).ToCharArray() Dim title As String = "Suggestions for '" & New String(word) & "'" Dim content As String If m_RichTextView.SpellChecker.IsCorrect(word, 0, word.Length - 1) = False Then Dim suggestions As IEnumerator(Of Char()) = m_RichTextView.SpellChecker.GetSuggestions(word, 0, word.Length - 1) Dim sb As StringBuilder = New StringBuilder() While suggestions.MoveNext() If sb.Length > 0 Then sb.Append(vbLf) End If sb.Append(suggestions.Current) End While content = sb.ToString() Else content = "The word is correct." End If NMessageBox.Show(m_RichTextView.DisplayWindow, content, title, ENMessageBoxButtons.OK) 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" #End Region End Class

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