我有一个RichTextBox,我想添加一个功能。基本上我启用了SpellCheck.isEnabled = True
,所以每当我拼错一些东西时,都会显示红色的波浪线。
我要添加的功能是"添加Word"特征。基本上我正在关注这个Tutorial
,它向您展示了如何做到这一点。本教程使用的是WPF TextBox
,而不是WPF RichTextBox
,因此我所做的就是尽力使其适用于WPF RichTextBox
。我的代码是:
private void ctrl_RtfText_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
int index = 0;
this.ctrl_RtfText.ContextMenu.Items.Clear(); //Clearing the existing items
//Getting the spellcheck suggestions.
SpellingError spellingError = this.ctrl_RtfText.GetSpellingError(this.ctrl_RtfText.CaretPosition);
if (spellingError != null && spellingError.Suggestions.Count() >= 1)
{
//Creating the suggestions menu items.
foreach (string suggestion in spellingError.Suggestions)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = suggestion;
menuItem.FontWeight = FontWeights.Bold;
menuItem.Command = EditingCommands.CorrectSpellingError;
menuItem.CommandParameter = suggestion;
menuItem.CommandTarget = this.ctrl_RtfText;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, menuItem);
index++;
}
Separator seperator = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator);
index++;
//Adding the IgnoreAll menu item
MenuItem IgnoreAllMenuItem = new MenuItem();
IgnoreAllMenuItem.Header = "Ignore All";
IgnoreAllMenuItem.Command = EditingCommands.IgnoreSpellingError;
IgnoreAllMenuItem.CommandTarget = this.ctrl_RtfText;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, IgnoreAllMenuItem);
index++;
}
else
{
//No Suggestions found, add a disabled NoSuggestions menuitem.
MenuItem menuItem = new MenuItem();
menuItem.Header = "No Suggestions";
menuItem.IsEnabled = false;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, menuItem);
index++;
}
//.Net 4.0 Supports CustomDictionaries, Option for Adding to dictionary.
TextPointer selectionStart = ctrl_RtfText.Document.ContentStart;
if (!(this.ctrl_RtfText.Selection.IsEmpty))
{
Separator seperator1 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator1);
index++;
MenuItem AddToDictionary = new MenuItem();
AddToDictionary.Header = "Add to Dictionary";
//Getting the word to add
this.ctrl_RtfText.GetSpellingErrorRange(selectionStart);
//Ignoring the added word.
AddToDictionary.Command = EditingCommands.IgnoreSpellingError;
AddToDictionary.CommandTarget = this.ctrl_RtfText;
AddToDictionary.Click += (object o, RoutedEventArgs rea) =>
{
this.AddToDictionary(this.ctrl_RtfText.Selection.Text);
};
this.ctrl_RtfText.ContextMenu.Items.Insert(index, AddToDictionary);
index++;
}
//Common Edit MenuItems.
Separator seperator2 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator2);
index++;
//Cut
MenuItem cutMenuItem = new MenuItem();
cutMenuItem.Command = ApplicationCommands.Cut;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, cutMenuItem);
index++;
//Copy
MenuItem copyMenuItem = new MenuItem();
copyMenuItem.Command = ApplicationCommands.Copy;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, copyMenuItem);
index++;
//Paste
MenuItem pasteMenuItem = new MenuItem();
pasteMenuItem.Command = ApplicationCommands.Paste;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, pasteMenuItem);
index++;
Separator seperator3 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator3);
index++;
//Delete
MenuItem deleteMenuItem = new MenuItem();
deleteMenuItem.Command = ApplicationCommands.Delete;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, deleteMenuItem);
index++;
Separator seperator4 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator4);
index++;
//Select All
MenuItem selectAllMenuItem = new MenuItem();
selectAllMenuItem.Command = ApplicationCommands.SelectAll;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, selectAllMenuItem);
index++;
}
//Method to Add text to Dictionary
private void AddToDictionary(string entry)
{
using (StreamWriter streamWriter = new StreamWriter(@"C:\Users\xxx\Desktop\New folder (2)\MyCustomDictionary.lex", true))
{
streamWriter.WriteLine(entry);
}
}
现在问题:
基本上是拼写检查器,当拼写检查器拼写错误时,右键单击该单词时,会显示上下文菜单并显示建议。用户可以选择其中一个建议和单词修复。这一切都发生在用户不需要突出显示该单词然后选择建议的情况下。让我说清楚:
用户点击首选建议并进行更正。现在我的代码基本上只显示"添加Word"选择文本时的功能:
我想要它,就像上一张图片一样,当出现错误时,"添加Word"功能应该与其他建议等一起出现。
答案 0 :(得分:0)
下载EN_US.lang,然后让它读取文件,然后添加单词 评论,如果你想让我告诉你如何做到这一点!
答案 1 :(得分:0)
要修复此错误,请替换
SpellingError spellingError = this.ctrl_RtfText.GetSpellingError(this.ctrl_RtfText.CaretPosition);
if (spellingError != null && spellingError.Suggestions.Count() >= 1)
{
//Creating the suggestions menu items.
foreach (string suggestion in spellingError.Suggestions)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = suggestion;
menuItem.FontWeight = FontWeights.Bold;
menuItem.Command = EditingCommands.CorrectSpellingError;
menuItem.CommandParameter = suggestion;
menuItem.CommandTarget = this.ctrl_RtfText;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, menuItem);
index++;
}
Separator seperator = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator);
index++;
//Adding the IgnoreAll menu item
MenuItem IgnoreAllMenuItem = new MenuItem();
IgnoreAllMenuItem.Header = "Ignore All";
IgnoreAllMenuItem.Command = EditingCommands.IgnoreSpellingError;
IgnoreAllMenuItem.CommandTarget = this.ctrl_RtfText;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, IgnoreAllMenuItem);
index++;
}
else
{
//No Suggestions found, add a disabled NoSuggestions menuitem.
MenuItem menuItem = new MenuItem();
menuItem.Header = "No Suggestions";
menuItem.IsEnabled = false;
this.ctrl_RtfText.ContextMenu.Items.Insert(index, menuItem);
index++;
}
//.Net 4.0 Supports CustomDictionaries, Option for Adding to dictionary.
TextPointer selectionStart = ctrl_RtfText.Document.ContentStart;
if (!(this.ctrl_RtfText.Selection.IsEmpty))
{
Separator seperator1 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Insert(index, seperator1);
index++;
MenuItem AddToDictionary = new MenuItem();
AddToDictionary.Header = "Add to Dictionary";
//Getting the word to add
this.ctrl_RtfText.GetSpellingErrorRange(selectionStart);
//Ignoring the added word.
AddToDictionary.Command = EditingCommands.IgnoreSpellingError;
AddToDictionary.CommandTarget = this.ctrl_RtfText;
AddToDictionary.Click += (object o, RoutedEventArgs rea) =>
{
this.AddToDictionary(this.ctrl_RtfText.Selection.Text);
};
this.ctrl_RtfText.ContextMenu.Items.Insert(index, AddToDictionary);
index++;
}
//REST OF THE CODE
与
SpellingError spellingError = this.ctrl_RtfText.GetSpellingError(this.ctrl_RtfText.CaretPosition);
if (spellingError != null)
{
//Creating the suggestions menu items.
foreach (string suggestion in spellingError.Suggestions)
{
MenuItem menuItem = new MenuItem();
menuItem.Header = suggestion;
menuItem.FontWeight = FontWeights.Bold;
menuItem.Command = EditingCommands.CorrectSpellingError;
menuItem.CommandParameter = suggestion;
menuItem.CommandTarget = this.ctrl_RtfText;
this.ctrl_RtfText.ContextMenu.Items.Add(menuItem);
}
if(this.ctrl_RtfText.ContextMenu.Items.Count == 0)
{
//No Suggestions found, add a disabled NoSuggestions menuitem.
MenuItem menuItem = new MenuItem();
menuItem.Header = "No Suggestions";
menuItem.IsEnabled = false;
this.ctrl_RtfText.ContextMenu.Items.Add(menuItem);
}
Separator seperator = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Add(seperator);
//Adding the IgnoreAll menu item
MenuItem IgnoreAllMenuItem = new MenuItem();
IgnoreAllMenuItem.Header = "Ignore All";
IgnoreAllMenuItem.Command = EditingCommands.IgnoreSpellingError;
IgnoreAllMenuItem.CommandTarget = this.ctrl_RtfText;
this.ctrl_RtfText.ContextMenu.Items.Add(IgnoreAllMenuItem);
Separator seperator1 = new Separator();
this.ctrl_RtfText.ContextMenu.Items.Add(seperator1);
MenuItem AddToDictionary = new MenuItem();
AddToDictionary.Header = "Add to Dictionary";
//Getting the word to add
var tr = this.ctrl_RtfText.GetSpellingErrorRange(this.ctrl_RtfText.CaretPosition);
//Ignoring the added word.
AddToDictionary.Command = EditingCommands.IgnoreSpellingError;
AddToDictionary.CommandTarget = this.ctrl_RtfText;
AddToDictionary.Click += (object o, RoutedEventArgs rea) =>
{
this.AddToDictionary(tr.Text);
};
this.ctrl_RtfText.ContextMenu.Items.Add(AddToDictionary);
}
//REST OF THE CODE
这是一个快速编辑。不确定它是否在没有一些小调整的情况下运行。