【问题标题】:Problem with finding the next word in RichTextBox在 RichTextBox 中查找下一个单词的问题
【发布时间】:2010-04-13 04:53:08
【问题描述】:

当我在我的 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) 如果拼写错误,它会给我下一个单词的第一个字母。那么根据拼写,只有其中一个有效?

我只是想知道您是否对这个问题有任何想法?我在这里做错了什么吗?

【问题讨论】:

  • 我不明白拼写是如何起作用的?
  • 好吧,我的意思是,如果 TextPointer 指向一个拼写正确与否的单词的开头。所以,我增加了TextPointer,直到我到达一个字符。然后,该字符是一个可以/不能正确拼写的单词的开头。因此,当拼写不正确时,我遇到了这个问题。

标签: c# .net wpf richtextbox


【解决方案1】:

我通过使用 Offset 解决了这个问题,因为这与它的拼写方式无关。这与我们添加任何文本后会跳转TextPointer的偏移有关。

所以这里是修复:

int Index = RichTextBox.CaretPosition.DocumentStart.GetOffsetToPosition(RichTextBox.CaretPosition);

TextPointer ptr1= RichTextBox.CaretPosition.DocumentStart.GetPositionAtOffset(Index);

char nextChar = GetNextChar();
//we continue until there is a character
while (char.IsWhiteSpace(nextChar))
{
   Index++;
   ptr1= RichTextBox.CaretPosition.DocumentStart.GetPositionAtOffset(Index);
   nextChar = GetCharacterAt(Ptr1);
}
//so now ptr1 is pointing to a character, and we do something with that TextPointer
ChangeFormat(ptr1);

【讨论】:

  • GetNextChar() 和 GetCharacterAt() 方法属于什么类?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多