Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBox.AppendText() not autoscrolling

Tags:

c#

.net

textbox

I tried the following to get my Textbox text to automatically scroll:

The steps I am using are pretty trivial:

  1. Drag textbox onto form.
  2. Change textbox to be multiline.
  3. Add vertical scroll.
  4. Use AppendText() to add text to the textbox.

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.

like image 857
gonzobrains Avatar asked Aug 15 '13 19:08

gonzobrains


People also ask

How do I scroll a text box in C#?

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.

How do you scroll to the bottom in C#?

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.


1 Answers

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();
like image 100
gonzobrains Avatar answered Sep 28 '22 05:09

gonzobrains