且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows窗体RichTextBox的光标位置

更新时间:2023-12-06 15:32:04

您可以存储光标位置,在更改前,再后来恢复它:

You can store the cursor position before making the change, and then restore it afterwards:

int i = richTextBox1.SelectionStart;
richTextBox1.Text += "foo";
richTextBox1.SelectionStart = i;

您可能还希望做同样的SelectionLength如果你不想删除的亮点。请注意,这可能会导致奇怪的行为如果插入的文本里面选择。然后,你将需要扩展选择以包含插入文本的长度。

You might also want to do the same with SelectionLength if you don't want to remove the highlight. Note that this might cause strange behaviour if the inserted text is inside the selection. Then you will need to extend the selection to include the length of the inserted text.