I've implemented a find function on a RichEditBox that when executed will search for query
and select the found text inside the RichEditBox:
String^ docText;
currentRichEditBox->Document->GetText(Text::TextGetOptions::None, &docText);
start = currentRichEditBox->Document->Selection->EndPosition;
end = docText->Length();
int result = newRange->FindText(query, end-start, Text::FindOptions::None);
if (result != 0)
{
currentRichEditBox->Document->Selection->SetRange(newRange->StartPosition, newRange->EndPosition);
}
This works, in that the text that is found is properly selected. However, for a RichEditBox whose contents are so long as to scroll, this will be off-screen and the RichEditBox won't scroll to bring it into view. Oddly enough, however, if this code is re-run then it'll scroll into view the previous result. For example, take the following text:
This is a test
[Screen end]
1 hat
2 hat
When my code searches for hat, it will successfully highlight the first instance of the word hat. However, the RichEditBox won't scroll down to it, even though it's off-screen. The second time the code is run, it will both highlight the second instance and scroll the screen down to the first instance of the word hat.
I am assuming there is a bug in the code for RichEditBox that is causing this. Is there any programmatic way I can manually scroll the RichEditBox or its implicit ScrollViewer to bring the caret into view?
To scroll the highlighted text into position, you'll need to use the ScrollIntoView
method available on the ITextRange
interface.