代码之家  ›  专栏  ›  技术社区  ›  paradisonoir

在richtextbox中查找下一个单词时出现问题

  •  0
  • paradisonoir  · 技术社区  · 14 年前

    当我在richtextbox中输入一个字符时,我想从它的文本范围中获取下一个字符。

    我是这样做的:

    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,从文本指针中,我得到文本范围,并进行修改。

    问题就在这里?

    当下一个单词拼写正确时,我没有问题,但是 如果拼写不正确 然后ptr1不会指向下一个单词的第一个字符(第二个字符),如果我使用getNextContextPosition(logicalDirection.Forward),如果拼写错误,它会给我下一个单词的第一个字母。所以根据拼写只有一个有效?

    我只是想知道你是否知道这个问题?我在这里做错什么了吗?

    1 回复  |  直到 14 年前
        1
  •  1
  •   paradisonoir    14 年前

    我用偏移量解决了这个问题,因为这与拼写方式无关。这与添加任何文本后它偏移textpointer将被跳过有关。

    所以这里是解决方法:

    int index=richtextbox.caretposition.documentstart.getOffsetPosition(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);