简体   繁体   中英

How set wpf richTextBox cursor position on the start

how do I set cursor position in richtextbox on start?

EDIT:

I wrote something in rtb2 then I clicked on enter and on event enter clicked is text sent do DB. Then I call this block of code:

            rtb2.Document.Blocks.Clear();

            TextPointer caretPos = rtb2.CaretPosition;

            caretPos = caretPos.DocumentStart;

            rtb2.CaretPosition = caretPos;

I want sent with this code, position on start. But cursor is move on second line.

You should use the Select method of the RichTextBox.Selection property. Pass inn two TextPointer object that both reference the start of the text. Look at this example from MSDN .

You need to set the CaretPosition property of the RichTextBox control. This is essentially the same thing as calling the Select method, and you still have to use TextPointer objects to indicate where you want to position the cursor. This is because rich text boxes can contain other types of elements than just plain text, so indices are not sufficient to correctly specify the location of the cursor.

If you want to set it when your text box is displayed, you should place the code in the Load event handler for the form that contains the RichTextBox control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM