Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

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.
C#
// 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.
 
Share this answer
 
Comments
Gssankar 12-Apr-14 3:17am    
Thanks Houghtelin for your response. i ill check

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900