Windows窗体RichTextBox的光标位置

编程入门 行业动态 更新时间:2024-10-12 18:22:35
本文介绍了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的光标位置

本文发布于:2023-10-08 01:12:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   光标   位置   Windows   RichTextBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!