windowsforms-114007-controls-and-libraries-spell-checker-examples-how-to-add-spell-check-menu-to-the-standard-text-controls.md
This example demonstrates how to show the WinForms SpellChecker context menu for standard .NET TextBox and RichTextBox controls.
The BarManager component is used to create and show the popup menu. For this, the BarManager.QueryShowPopupMenu event is handled. Menu items are obtained with the SpellChecker.GetCommandsByError method and added to the PopupMenuBase.ItemLinks collection of the newly created popup menu. The BarManager.SetPopupContextMenu method assigns the popup menu to the specified TextBox and RichTextBox controls.
The MemoEdit control located on the same form has built-in SpellChecker support and does not require extra code to show the SpellChecker context menu.
The SpellCheckerBase.PrepareSuggestions event is handled to modify suggested words displayed in the SpellChecker context menu.
using DevExpress.XtraBars;
using DevExpress.XtraSpellChecker;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace SpellCheckerMenuExample
{
public partial class Form1 : Form
{
PopupMenu popupMenu1 = new PopupMenu();
BarManager barManager1 = new BarManager();
public Form1()
{
InitializeComponent();
spellChecker1.PrepareSuggestions += spellChecker1_PrepareSuggestions;
barManager1.Form = this;
}
private void Form1_Load(object sender, EventArgs e)
{
spellChecker1.SpellCheckMode = SpellCheckMode.AsYouType;
spellChecker1.ParentContainer = this;
spellChecker1.CheckAsYouTypeOptions.CheckControlsInParentContainer = true;
popupMenu1.Manager = barManager1;
barManager1.QueryShowPopupMenu += barManager1_QueryShowPopupMenu;
// Add spelling menu to the standard controls.
barManager1.SetPopupContextMenu(textBox1, popupMenu1);
barManager1.SetPopupContextMenu(richTextBox1, popupMenu1);
}
private void barManager1_QueryShowPopupMenu(object sender, QueryShowPopupMenuEventArgs e)
{
Point position = e.Control.PointToClient(e.Position);
DevExpress.XtraSpellChecker.Rules.SpellCheckErrorBase error = spellChecker1.CalcError(position);
e.Cancel = error == null;
List<SpellCheckerCommand> commands = spellChecker1.GetCommandsByError(error);
if (commands != null)
{
popupMenu1.ItemLinks.Clear();
foreach (SpellCheckerCommand command in commands)
{
BarButtonItem item = new BarButtonItem(barManager1, command.Caption);
item.Enabled = command.Enabled;
item.Tag = command;
item.ItemClick += new ItemClickEventHandler(OnPopupMenuItemClick);
popupMenu1.ItemLinks.Add(item);
}
BarButtonItem itemShowSpellingForm = new BarButtonItem(barManager1, "Show Spelling Form");
itemShowSpellingForm.ItemClick += OnPopupMenuShowSpellingForm_ItemClick;
popupMenu1.ItemLinks.Add(itemShowSpellingForm);
}
}
void OnPopupMenuItemClick(object sender, ItemClickEventArgs e)
{
(e.Item.Tag as SpellCheckerCommand).DoCommand();
}
void OnPopupMenuShowSpellingForm_ItemClick(object sender, ItemClickEventArgs e)
{
spellChecker1.Check(this.ActiveControl);
}
#region #preparesuggestions_event
void spellChecker1_PrepareSuggestions(object sender, PrepareSuggestionsEventArgs e)
{
string pattern = "(De|de)[a-z]+x[a-z]*s+";
System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(pattern);
if (rgx.IsMatch(e.Word)) e.Suggestions.Insert(0, new SuggestionBase("DevExpress", 0));
}
#endregion #preparesuggestions_event
}
}
Imports DevExpress.XtraBars
Imports DevExpress.XtraSpellChecker
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Namespace SpellCheckerMenuExample
Partial Public Class Form1
Inherits Form
Private popupMenu1 As New PopupMenu()
Private barManager1 As New BarManager()
Public Sub New()
InitializeComponent()
AddHandler spellChecker1.PrepareSuggestions, AddressOf spellChecker1_PrepareSuggestions
barManager1.Form = Me
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
spellChecker1.SpellCheckMode = SpellCheckMode.AsYouType
spellChecker1.ParentContainer = Me
spellChecker1.CheckAsYouTypeOptions.CheckControlsInParentContainer = True
popupMenu1.Manager = barManager1
AddHandler barManager1.QueryShowPopupMenu, AddressOf barManager1_QueryShowPopupMenu
' Add spelling menu to the standard controls.
barManager1.SetPopupContextMenu(textBox1, popupMenu1)
barManager1.SetPopupContextMenu(richTextBox1, popupMenu1)
End Sub
Private Sub barManager1_QueryShowPopupMenu(ByVal sender As Object, ByVal e As QueryShowPopupMenuEventArgs)
Dim position As Point = e.Control.PointToClient(e.Position)
Dim [error] As DevExpress.XtraSpellChecker.Rules.SpellCheckErrorBase = spellChecker1.CalcError(position)
e.Cancel = [error] Is Nothing
Dim commands As List(Of SpellCheckerCommand) = spellChecker1.GetCommandsByError([error])
If commands IsNot Nothing Then
popupMenu1.ItemLinks.Clear()
For Each command As SpellCheckerCommand In commands
Dim item As New BarButtonItem(barManager1, command.Caption)
item.Enabled = command.Enabled
item.Tag = command
AddHandler item.ItemClick, AddressOf OnPopupMenuItemClick
popupMenu1.ItemLinks.Add(item)
Next command
Dim itemShowSpellingForm As New BarButtonItem(barManager1, "Show Spelling Form")
AddHandler itemShowSpellingForm.ItemClick, AddressOf OnPopupMenuShowSpellingForm_ItemClick
popupMenu1.ItemLinks.Add(itemShowSpellingForm)
End If
End Sub
Private Sub OnPopupMenuItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
TryCast(e.Item.Tag, SpellCheckerCommand).DoCommand()
End Sub
Private Sub OnPopupMenuShowSpellingForm_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
spellChecker1.Check(Me.ActiveControl)
End Sub
#Region "#preparesuggestions_event"
Private Sub spellChecker1_PrepareSuggestions(ByVal sender As Object, ByVal e As PrepareSuggestionsEventArgs)
Dim pattern As String = "(De|de)[a-z]+x[a-z]*s+"
Dim rgx As New System.Text.RegularExpressions.Regex(pattern)
If rgx.IsMatch(e.Word) Then
e.Suggestions.Insert(0, New SuggestionBase("DevExpress", 0))
End If
End Sub
#End Region ' #preparesuggestions_event
End Class
End Namespace