【发布时间】:2018-05-11 12:36:32
【问题描述】:
我尝试实现 Hunspell 拼写检查器库,以便在我使用 c# 在记事本应用程序中键入时检查我的拼写。它似乎可以正常工作,但是当出现拼写错误的单词时,整个 RichTextBox 都会加下划线。
public void spellchecker()
{
Invoke(new MethodInvoker(delegate ()
{
using (Hunspell hunspell = new Hunspell("en_us.aff", "en_US.dic"))
{
String sentence = GetRichTextBox().Text;
foreach (string item in sentence.Split(' '))
{
bool correct = hunspell.Spell(item);
if (correct == false)
{
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Underline);
}
else {
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Regular);
}
}
}
}));
}
错误似乎在以下行:
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Underline);
所以当我将其替换为:
item.Font = new Font(item.Font, FontStyle.Underline);
..出现“字符串不包含字体定义”的错误。我无法将拼写错误的单词单独加下划线。
【问题讨论】:
-
GetRichTextBox().Font改变整个 RichTextBox 的字体。item.Font无效,因为 item 只是一个没有Font属性的字符串。您的问题应该类似于 "How to change the font for only part of the text of RichTextBox". -
这是 WinForms 还是 WPF?
-
这是一个 Windows 窗体应用程序
-
“不起作用”没有任何意义。编辑您的问题并在其他问题的帮助下显示您尝试过的内容以及您可能遇到的任何错误消息。
标签: c# .net winforms richtextbox