Windows窗体RichTextBox的光标位置 [英] Windows Forms RichTextBox cursor position

查看:462
本文介绍了Windows窗体RichTextBox的光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一个RichTextBox控制一个C#Windows窗体程序。每当箱内的文本被改变(比打字这种改变其他),光标返回到开始处。

I have a C# Windows Forms program that has a RichTextBox control. Whenever the text inside the box is changed (other than typing that change), the cursor goes back to the beginning.

在换句话说,当在在RichTextBox文本是通过使用Text属性改变,它使光标跳回来。

In other words, when the text in the RichTextBox is changed by using the Text property, it makes the cursor jump back.

我如何才能让光标在同一位置或编辑的文本一起移动?

How can I keep the cursor in the same position or move it along with the edited text?

感谢

推荐答案

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

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.

这篇关于Windows窗体RichTextBox的光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆