johnvms

This seems like it should be easy, but I'm just not finding anything that will work. Perhaps there is a better initial approach, but here is what's going on:

I want to programatically insert some text into a RichTextBox at the current position and have the caret/insertion point move just past the new text. Right now I'm doing:

_richTextBox.CaretPosition.InsertTextInRun(newText);

However, the caret doesn't move, so it appear just before the new text. I tried:

TextPointer newPointer = _richTextBox.CaretPosition.GetPositionOffset(newText.Length);
_richTextBox.Selection.Select(newPointer,newPointer);

If works some of the time, but others it doesn't position things correctly.

I could use the clipboard and use the Paste method, but don't want to modify the clipboard contents in this situation, plus that seems like a lot of overhead for what should be a simple operation.

Any ideas on what I'm missing here.

Thanks



Re: Windows Presentation Foundation (WPF) RichTextBox - how to update insert position after InsertTextInRun

lee d

you can try

rtb1.CaretPosition.InsertTextInRun(s); //s is the variable with some text

rtb1.CaretPosition = rtb1.CaretPosition.GetPositionAtOffset(s.Length, LogicalDirection.Forward);

rtb1.Focus();






Re: Windows Presentation Foundation (WPF) RichTextBox - how to update insert position after InsertTextInRun

johnvms

That was it!!!! I knew it was something simple. For some reason I thought CaretPosition was read only. Missed the 'set' when scanning the docs.

Thank you VERY much!