如何在富文本框中保存和恢复光标线poistion [英] how to save and restore the cursor line poistion in rich textbox

查看:171
本文介绍了如何在富文本框中保存和恢复光标线poistion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何在richtextbox中保存和恢复光标线和位置。



i放置win形式的richtextbox和ai放置一个包含行号的标签。如果我们保存位置并滚动,如果我们想恢复,我们需要滚动到原始位置。



谢谢

GSS

Hi

How to save and restore the cursor line and position in richtextbox.

i placed a richtextbox in win form and a i placed a label which contains line number. if we save a position and scroll if we want to restore we need to scroll to the original position.

Thanks
GSS

推荐答案

以下是一些以您描述的方式导航的基本方法。您可以增加位置变量的范围,甚至可以将其保存为用户设置,以便编辑器可以恢复到用户重新启动程序时的位置。

Here are some basic methods to navigate in the manner that you describe. You can increase the scope of the position variable or even save it as a user setting so that the editor can resume to the position when the user restarts the program.
// Get the position
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    // Capture position    
    Point curPos = Cursor.Position;
    // Place position in textbox
    textBox1.Text  = richTextBox1.GetCharIndexFromPosition(curPos).ToString();
}

// Set and scroll to the position
private void button1_Click(object sender, EventArgs e)
{
    // Subtract 1 from stored position to restore to original position.
    richTextBox1.SelectionStart = int.Parse(textBox1.Text) - 1; 
    richTextBox1.SelectionLength = 0;
    // Scroll to position
    richTextBox1.ScrollToCaret();
    // Return focus to RTB
    richTextBox1.Focus();
}



祝你好运。


Good luck.


这篇关于如何在富文本框中保存和恢复光标线poistion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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