I tried the following to get my Textbox text to automatically scroll:
The steps I am using are pretty trivial:
The text does not automatically scroll, despite trying to solutions mentioned here:
How do I automatically scroll to the bottom of a multiline text box?
What could cause this and how do I fix it?
UPDATE: If I create a button and use it to call AppendText() I get the desired behavior. However, if I try to call AppendText from the form's constructor or Load() event then I get the appended text but the TextBox does not scroll. This is NOT a duplicate question as I haven't seen anyone post this problem in the past.
Step 1 : Create a textbox using the TextBox() constructor provided by the TextBox class. // Creating textbox TextBox Mytextbox = new TextBox(); Step 2 : After creating TextBox, set the ScrollBars property of the TextBox provided by the TextBox class. // Set ScrollBars property Mytextbox.
Document. Body. ScrollIntoView(false); The boolean parameter for ScrollIntoView () is true to align the scrollbar with the top of the document, and false to align the scrollbar with the bottom of the document.
Since the form isn't quite ready during the constructor and load event, I had to use a task to get it to scroll after it becomes ready:
Here is the method that gets invoked:
void scroll()
{
this.Invoke(new MethodInvoker(delegate()
{
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
}));
}
It gets invoked via this task placed in the load event:
Task task1 = new Task(new Action(scroll));
task1.Start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With