拼写检查只替换文本框中的第一个单词
本文关键字:第一个 单词 文本 检查 替换 | 更新日期: 2023-09-27 17:49:33
我知道我以前在某个地方见过这个问题,但我不确定当时是否有答案。我正试图将拼写检查添加到WPF, . net 4.0中的TextBox
。它在查找和标记错误单词方面工作良好,如果TextBox
中的第一个单词不正确,它将替换它。任何超过单词1的东西,它只是将克拉移动到TextBox
的开始而不改变任何东西?就像我说的,我在6-9个月前在某个地方看到过这个,但现在我在谷歌上想到的所有东西都是用其他语言处理的(我现在严格地用英语)。我包含事件方法和样式化XAML只是为了完整性,我不认为问题在那里。
XAML:
<MultiBox:MultiBox Name="callNotes" Grid.Column="1" Width="Auto" Height="Auto" Margin="2,5,15,20" VerticalAlignment="Stretch" AcceptsReturn="True" FontWeight="Bold" GotFocus="callNotes_GotFocus" SelectAllOnGotFocus="False" SpellCheck.IsEnabled="True" xml:lang="en-US" Style="{StaticResource TextBoxStyle}" TextChanged="callNotes_TextChanged" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" />
<Style x:Key="TextBoxStyle" TargetType="{x:Type MyNamespace:MultiBox}">
<Setter Property="CharacterCasing" Value="Upper" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Height" Value="23" />
<Setter Property="Width" Value="Auto" />
<Setter Property="SelectAllOnGotFocus" Value="True" />
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
代码:
private void callNotes_TextChanged(object sender, TextChangedEventArgs e)
{
callNotes.Text.ToUpper();
lineCountOne.Content = ((callNotes.Text.Length / 78) + 1);
}
private void callNotes_GotFocus(object sender, RoutedEventArgs e)
{
callNotes.CaretIndex = callNotes.Text.Length;
}
这将有助于查看试图纠正错误的代码。下面是一个简单的代码,它循环遍历所有检测到的错误并接受第一个建议。如果您只想修复特定的错误,则需要通过获取特定索引处的错误来跳转到您感兴趣的特定错误。
int ndx;
while ((ndx = callNotes.GetNextSpellingErrorCharacterIndex(0, LogicalDirection.Forward)) != -1)
{
var err = callNotes.GetSpellingError(ndx);
foreach (String sugg in err.Suggestions)
{
err.Correct(sugg);
break;
}
}
在尝试了jschroedl的建议后,仍然没有运气(尽管我知道他的答案应该是正确的),我开始尝试我能想到的每一个可能的设置,甚至创建了一个全新的WPF项目,Spellcheck
启用TextBox
只是为了确保它不是Visual Studio/的东西。NET安装本身。事实证明并非如此,这是我几个月前做过的事情,以确保通过程序选择任何给定的TextBox
都会导致触发SelectAll()
方法。一旦我从这段代码中筛选出这个特定的TextBox
,一切都很好。再次感谢jschroedl,我知道他不可能知道这些。下面是违规代码,以防有人遇到类似的问题。
protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true);
base.OnStartup(e);
}
protected static void SelectAllText(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null && textBox.Name != "callNotes")
textBox.SelectAll();
}
添加&&文本框。Name = "callNotes"解决了问题