views:

111

answers:

1

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?

A: 

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);

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);
paradisonoir