在 RichTextBox 中查找下一个单词时出现问题
当我在 RichTextBox 中输入一个字符时,我想从其 TextRange 中获取下一个字符。
所以我是这样做的:
TextPointer ptr1= RichTextBox.CaretPosition;
char nextChar = GetNextChar();
//we continue until there is a character
while (char.IsWhiteSpace(nextChar))
{
ptr1= ptr1.GetNextInsertionPosition(LogicalDirection.Forward);
nextChar = GetCharacterAt(Ptr1);
}
//so now ptr1 is pointing to a character, and we do something with that TextPointer
ChangeFormat(ptr1);
然后我获取下一个字符的 ptr1,并从 TextPointer 获取 TextRange,并进行更改。
那么问题就在这里?
当下一个单词拼写正确时,我没有问题,但是如果拼写不正确那么 ptr1 将不会指向该单词的第一个字符下一个单词(第二个字符),如果我使用 GetNextContextPosition(LogicalDirection.Forward) ,如果拼写错误,它会给出下一个单词的第一个字母。那么根据拼写,只有其中之一有效?
我只是想知道你对这个问题有什么想法吗?我在这里做错了什么吗?
As I enter a character in my RichTextBox, I want to get the next character from the its TextRange.
So here is how I do it:
TextPointer ptr1= RichTextBox.CaretPosition;
char nextChar = GetNextChar();
//we continue until there is a character
while (char.IsWhiteSpace(nextChar))
{
ptr1= ptr1.GetNextInsertionPosition(LogicalDirection.Forward);
nextChar = GetCharacterAt(Ptr1);
}
//so now ptr1 is pointing to a character, and we do something with that TextPointer
ChangeFormat(ptr1);
then I get the ptr1 of the next character and from the TextPointer, I get the TextRange, and do my changes.
So here is the problem?
when the next word is spelled correctly, I have no problem, but if it's not spelled properly then ptr1 would not point to the first character of the next word (the second character), and if I use GetNextContextPosition(LogicalDirection.Forward) it would give me the first letter of the next word if it's misspelled. So depending on the spelling only one of them works?
I was just wondering if you have any idea about this problem? Is there anything wrong I am doing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过使用 Offset 解决了这个问题,因为这与它的拼写方式无关。这与我们添加任何文本后,它的偏移量 TextPointer 会被跳转有关。
所以这里是修复:
int Index = RichTextBox.CaretPosition.DocumentStart.GetOffsetToPosition(RichTextBox.CaretPosition);
I fixed the problem by using Offset as this is not related to how it's been spelled. This is related to the fact that it offset TextPointer would be jumped after we add any text.
So here is the fix:
int Index = RichTextBox.CaretPosition.DocumentStart.GetOffsetToPosition(RichTextBox.CaretPosition);