yu yang Jian yu yang Jian -4 years ago 590
C# Question

Set Caret/Cursor Position in RichTextBox - WPF

How to set caret/cursor position in RichTextBox in WPF?

I use the code in MSDN CaretPosition but it seems the cursor can't be set?

// Create a new FlowDocument, and add 3 paragraphs.
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
// Set the FlowDocument to be the content for a new RichTextBox.
RichTextBox rtb = new RichTextBox(flowDoc);

// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;

// Set the TextPointer to the end of the current document.
caretPos = caretPos.DocumentEnd;

// Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos;

Answer Source

How to set caret/cursor position in RichTextBox in WPF?

Assuming thatrtb is the name of your RichTextBox, with different Blocks and Inlines, you can set the Caret at the beginning of the document by:

rtb.CaretPosition = rtb.CaretPosition.DocumentStart;

or at its end:

rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;

On the other hand, assuming that you have a specific Paragraph or Block, such as:

Block blk = rtb.Document.Blocks.ElementAt(1);

You can set caret to its start

rtb.CaretPosition = blk.ContentStart;

or its end

rtb.CaretPosition = blk.ContentEnd;

or if you have a specific Inline such as

Run r = ((Paragraph)rtb.Document.Blocks.ElementAt(0)).Inlines.ElementAt(1) as Run;

You can also use

rtb.CaretPosition = r.ContentStart;
rtb.CaretPosition = r.ContentEnd;

Of course, if you are working with Complex paragraph with both right-to-left and left-to-right text, you might need to consider

rtb.CaretPosition = blk.ElementStart;
rtb.CaretPosition = blk.ElementEnd;

Also note different methods implemented in a TextPointer, which you can use to reach different parts of the document/Blocks/Inlines:

rtb.CaretPosition = rtb.CaretPosition.GetLineStartPosition(0);
rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(2);

See the link for more methods and more information.

At the end, you might want to use BringIntoView method implemented in a Block or Inline:

blk.BringIntoView();
r.BringIntoView();

and also setting the keyboard focus, to see the blinking of the Caret:

Keyboard.Focus(rtb);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download