Use this RadRichTextBox subclass to add a context menu with spellcheck and normal (cut, copy, paste, etc) operations.
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
Telerik.WinControls;
using
System.Collections;
using
Telerik.WinControls.RichTextBox;
using
Telerik.WinControls.RichTextBox.Layout;
using
Telerik.WinControls.UI;
using
Telerik.WinControls.RichTextBox.Model;
using
System.Diagnostics;
using
System.Linq;
using
Telerik.WinControls.RichTextBox.Proofing;
class
MyRadRichTextBox : RadRichTextBox
{
private
void
ReplaceCurrentWord(SpanLayoutBox spanBox,
string
newWord)
{
this
.Document.CaretPosition.MoveToInline(spanBox, 0);
DocumentPosition endPosition =
new
DocumentPosition(
this
.Document.CaretPosition);
MoveToCurrentWordEnd(endPosition);
this
.Document.Selection.Clear();
this
.Document.Selection.AddSelectionStart(
this
.Document.CaretPosition);
this
.Document.Selection.AddSelectionEnd(endPosition);
this
.Insert(newWord);
}
/// <summary>
/// DocumentPosition.MoveToCurrentWorkEnd was including characters other than Alpha-numerics.
/// This function will stop at non alphanumeric characters.
/// </summary>
/// <param name="position"></param>
private
void
MoveToCurrentWordEnd(DocumentPosition position)
{
position.MoveToCurrentWordEnd();
this
.Document.Selection.Clear();
this
.Document.Selection.AddSelectionStart(
this
.Document.CaretPosition);
this
.Document.Selection.AddSelectionEnd(position);
string
selectedText =
this
.Document.Selection.GetSelectedText();
int
length = selectedText.Length;
int
indexOfEnd = selectedText.TakeWhile(c =>
char
.IsLetterOrDigit(c)).Count();
while
(indexOfEnd < length)
{
position.MoveToPrevious();
length--;
}
this
.Document.Selection.Clear();
}
private
RadDropDownMenu BuildContextMenu(SpanLayoutBox spanBox)
{
RadDropDownMenu menu =
new
RadDropDownMenu();
RadMenuItem menuItem;
if
(
this
.Document.Selection.IsEmpty &&
this
.IsSpellCheckingEnabled && spanBox !=
null
)
{
this
.Document.CaretPosition.MoveToInline(spanBox, 0);
string
spanBoxTextAlphaNumericOnly = String.Concat(spanBox.Text.TakeWhile(c =>
char
.IsLetterOrDigit(c)));
if
(spanBoxTextAlphaNumericOnly.Length > 0 && !
this
.SpellChecker.CheckWordIsCorrect(spanBoxTextAlphaNumericOnly))
{
string
[] suggestions = (
string
[])
this
.SpellChecker.GetSuggestions(spanBox.Text);
if
(suggestions.Length <= 0)
{
menuItem =
new
RadMenuItem(
"(No Spelling Suggestions)"
);
menuItem.Enabled =
false
;
menu.Items.Add(menuItem);
}
foreach
(
string
suggestion
in
suggestions)
{
menuItem =
new
RadMenuItem(suggestion);
menuItem.Click += (
object
sender, EventArgs e) => { ReplaceCurrentWord(spanBox, (sender
as
RadMenuItem).Text); };
menu.Items.Add(menuItem);
}
menu.Items.Add(
new
SeparatorElement());
menuItem =
new
RadMenuItem(
"Add to Dictionary"
);
menuItem.Click += (
object
sender, EventArgs e) => {
this
.SpellChecker.AddWord(spanBoxTextAlphaNumericOnly); };
menu.Items.Add(menuItem);
menu.Items.Add(
new
SeparatorElement());
}
}
menuItem =
new
RadMenuItem(
"Undo"
);
menuItem.Click += (
object
sender, EventArgs e) => {
this
.Undo(); };
menu.Items.Add(menuItem);
menu.Items.Add(
new
SeparatorElement());
menuItem =
new
RadMenuItem(
"Cut"
);
menuItem.Click += (
object
sender, EventArgs e) => {
this
.Cut(); };
menu.Items.Add(menuItem);
menuItem =
new
RadMenuItem(
"Copy"
);
menuItem.Click += (
object
sender, EventArgs e) => {
this
.Copy(); };
menu.Items.Add(menuItem);
menuItem =
new
RadMenuItem(
"Paste"
);
menuItem.Click += (
object
sender, EventArgs e) => {
this
.Paste(); };
menu.Items.Add(menuItem);
menuItem =
new
RadMenuItem(
"Delete"
);
menuItem.Click += (
object
sender, EventArgs e) => {
this
.Delete(
false
); };
menu.Items.Add(menuItem);
menu.Items.Add(
new
SeparatorElement());
menuItem =
new
RadMenuItem(
"Select All"
);
menuItem.Click += (
object
sender, EventArgs e) => {
this
.Document.Selection.SelectAll(); };
menu.Items.Add(menuItem);
return
menu;
}
protected
override
void
OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if
(e.Button == System.Windows.Forms.MouseButtons.Right)
{
SpanLayoutBox spanBox =
this
.Document.GetLayoutBoxByPosition(e.Location)
as
SpanLayoutBox;
BuildContextMenu(spanBox).Show(
this
, e.Location);
}
base
.OnMouseDown(e);
}
}
}