简体   繁体   中英

Position TextBox Caret/Cursor to Same Line as Other Textbox C#

I've been looking this up, but none of the methods I have tried worked.

I basically want to take the line the caret is on from one textbox, and scroll the second textbox to the same position.

This is what I tried:

        int line = textBox1.GetLineFromCharIndex(textBox1.SelectionStart);
        Point pNew = new Point();
        pNew.X = 0;
        pNew.Y = line;
        textBox2.SelectionStart = textBox2.GetCharIndexFromPosition(pNew);
        textBox2.ScrollToCaret();

All I want to do is keep the second textbox as scrolled as much as the first textbox.

Is this what you want :

       textBox2.SelectionStart = textBox1.SelectionStart;
       textBox2.ScrollToCaret();

this works where the two textboxes text are equal.

I didn't get your purpose of All I want to do is keep the second textbox as scrolled as much as the first textbox. I think: the two TextBoxes have the same Text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM