Andy
Andy

Reputation: 46324

How do you prevent a multiline textbox from scrolling when text is added?

I have a multiline TextBox called Console. While running, this textbox is being filled up with some communications data. I use

TextBox.AppendText("txt\r\n");

to add a line to it and that allows me to have it autoscroll down. My problem is I want to be able to not have it autoscroll down. So I thought I would try

TextBox.Text += "text";

But that scrolls you to the beginning of the box. My latest attempt was to use TextBox.SelectionStart to save the position before I wrote and restore it back to that after, but that didn't seem to make a difference and still brings me back to the beginning of the text.

int txtPosition = Console.SelectionStart;
Console.Text += "TextToAdd";
Console.SelectionStart = txtPosition;

Ideally I want to just be able to have the box stay where ever it happens to be and not scroll to the beginning or end of the text.

Upvotes: 4

Views: 3210

Answers (2)

LBushkin
LBushkin

Reputation: 131646

For a WinForms textbox, you may should able to do this:

textBox.SelectionStart = 0; 
textBox.ScrollToCaret(); // force current position back to top

Upvotes: 1

Doug
Doug

Reputation: 5318

I think you need to you a richtextbox instead of a generic textbox and this will provide you with the functionality you desire.

Enjoy!

Upvotes: 2

Related Questions