2

I am using RichEditBox for a rich text editor: I use different colors for different keywords in the text while user types the text in the richEditBox control.

Before making changes I save current selection position:

ITextSelection selection = richEditBox.Document.Selection;
int originalStartPosition = selection.StartPosition;
int originalEndPosition = selection.EndPosition;

then I change color of some parts of the text, for example:

selection.SetRange(startIndex, stopIndex);
selection.CharacterFormat.ForegroundColor = Colors.Red;

and after making all changes I restore current selection position:

selection.SetRange(originalStartPosition, originalEndPosition);

This works fine, but SetRange() function affects scrolling position of the text in richEditBox, because it automatically scrolls to make the selected part of the text visible on the screen. So, after all color modifications the scroll position of richEditBox control becomes modified.

Final SetRange() function call puts cursor in its initial place, but not the scroll position.

So, my question is: how can I restore scrolling position of richEditBox control after making color changes? Or how can I modify text color without affecting the scroll position?

Kibernetik
  • 2,947
  • 28
  • 35
  • That's strange, because you're saying that the first ´SetRange´ scrolls but the second doesn't - or do you mean, that the second range is still in view? Then you could take a look at this: http://stackoverflow.com/questions/626988/prevent-autoscrolling-in-richtextbox Because scrolling up again gets very ugly by time. – MetaColon Mar 20 '17 at 17:10
  • SetRange() function scrolls not to the initial position, but just to make selection visible on the screen. – Kibernetik Mar 20 '17 at 17:36
  • Then I'd just like to reference to the other question already answered I wrote a link to. I guess hiding the selection should solve the problem. – MetaColon Mar 20 '17 at 18:24
  • Thank you for your link, but I am making an UWP application, so the approach you mentioned does not suit. This should be done either in a much more simple way or be not possible to do at all. – Kibernetik Mar 20 '17 at 18:28
  • I'm sorry, I overlooked the uwp tag. But if your sure that the other approach from the link doesn't work, I don't think there's an easy solution. I'm not that familiar with uwp anyway, so I might be wrong though. – MetaColon Mar 20 '17 at 18:31
  • Yes, there are some solutions but they don't suit UWP API. – Kibernetik Mar 20 '17 at 18:35

1 Answers1

0

It seems it is by design. When we set the cursor in the RichEditBox, then we can scroll the text that we can not see the cursor. After we use the SetRange method, it will scroll to the position that we set the cursor. If the cursor is in the view, it will not scroll.

If you want to scroll to the text range that you set, you should be able to set the PointOptions.Start to the ScrollIntoView method. It will scroll the end of the text range into view.

For example:

ITextSelection selection = MyRichEditBox.Document.Selection;
int originalStartPosition = selection.StartPosition;
int originalEndPosition = selection.EndPosition;
selection.SetRange(600, 610);
selection.CharacterFormat.ForegroundColor = Colors.Red;
selection.SetRange(originalStartPosition, originalEndPosition);
selection.ScrollIntoView(PointOptions.Start);

Edit code:

ITextSelection selection = MyRichEditBox.Document.Selection;
int originalStartPosition = selection.StartPosition;
int originalEndPosition = selection.EndPosition;
selection.SetRange(600, 610);
selection.ScrollIntoView(PointOptions.Start);
await Task.Delay(2000);
selection.CharacterFormat.ForegroundColor = Colors.Red;
selection.SetRange(originalStartPosition, originalEndPosition);
Jayden
  • 3,276
  • 1
  • 10
  • 14
  • Thank you for your input! The problem is that ScrollIntoView() scrolls not to the same scroll position where editor was before first SetRange() was called. After series of SetRange() calls which move the cursor position out of view, final ScrollIntoView() brings cursor position back to the screen, but not to the position where it was in the beginning but to the position that cursor is just visible. But as this happens in real time while user types the text this leads to unpredictable jumps of text. – Kibernetik Mar 21 '17 at 14:27
  • Please try to add the delay method ` await Task.Delay(2000);` before you set the Color, it will scroll to the first SetRandge position then change the color. At last it will scroll to the range that you selected. It seems we should be able to move the `ScrollIntoView` after first SetRange. See my edit code. – Jayden Mar 22 '17 at 02:01