5

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.

14
  • 1
    Care to share the code that you tried? Commented Aug 15, 2013 at 20:02
  • 2
    I think you got downvotes because you don't provide any detail. The question says "the solutions...do not work for me". This isn't very descriptive. Give us a complete example to reproduce the problem and specific details on how it isn't working. Commented Aug 15, 2013 at 20:25
  • 1
    @Amy It is pretty simple. I just call myTextBox.AppendText("my text to display") but it doesn't automatically scroll the window. I thought this was pretty trivial, even creating a sample program to prove I wasn't crazy. It turns out that I can use a button click to append text but if I try to append text from elsewhere the text does not automatically scroll (see my question's update). Commented Aug 15, 2013 at 21:33
  • 5
    Try the Shown event then. Commented Aug 15, 2013 at 21:38
  • 1
    Are you looking for Activated? Commented Aug 15, 2013 at 21:58

2 Answers 2

11

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(); 
Sign up to request clarification or add additional context in comments.

2 Comments

A cleaner approach to doing intialization tasks when the form is ready: stackoverflow.com/a/219155/388994
Actually, there is no need to create a task for it. Add the code block next to wherever you append a text to the related textbox.
2

You can also try TextBox.ScrollToEnd() function if ScrollToCaret() doesn't work.

txtBox1.AppendText("somthing"); txtBox1.ScrollToEnd(); 

1 Comment

This doesn't work for me - "ScrollToEnd" is not a method of TextBox. I'm using C#, Winforms and the .NET Framework 4.6.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.