简体   繁体   English

如何在开始时设置WPF RichTextBox光标位置

[英]How set wpf richTextBox cursor position on the start

how do I set cursor position in richtextbox on start? 如何在开始时在richtextbox中设置光标位置?

EDIT: 编辑:

I wrote something in rtb2 then I clicked on enter and on event enter clicked is text sent do DB. 我在rtb2中写了一些东西,然后单击Enter,然后在事件进入时单击,单击是发送给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. 您应该使用RichTextBox.Selection属性的Select方法。 Pass inn two TextPointer object that both reference the start of the text. 传递两个引用文本开头的两个TextPointer对象。 Look at this example from MSDN . 从MSDN看这个例子

You need to set the CaretPosition property of the RichTextBox control. 您需要设置RichTextBox控件的CaretPosition属性。 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. 这本质上与调用Select方法相同,并且您仍然必须使用TextPointer对象来指示要将光标放置在何处。 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. 如果要在显示文本框时进行设置,则应将代码放置在包含RichTextBox控件的窗体的Load事件处理程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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