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



Question:

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.


Solution:1

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

Enjoy!


Solution:2

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

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »