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.

Community
  • 1
  • 1
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • Did you try this? This is what I´ve been forced to used in the past: myTextBox.SelectionStart = myTextBox.Text.Length; myTextBox.ScrollToCaret(); – Marcus Aug 15 '13 at 19:57
  • 1
    Care to share the code that you tried? – Austin Salonen Aug 15 '13 at 20:02
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Aug 15 '13 at 20:04
  • try using a [richTextBox](http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx), it has a scrollbar built in. – Chris Aug 15 '13 at 20:18
  • I set the TextBox vertical scrollbar to be visible. I can manually scroll it but I want it to automatically scroll to the bottom everytime new text is added. – gonzobrains Aug 15 '13 at 20:20
  • 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. –  Aug 15 '13 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). – gonzobrains Aug 15 '13 at 21:33
  • 5
    Try the `Shown` event then. – LarsTech Aug 15 '13 at 21:38
  • @LarsTech I thought about that, but I don't want this text duplicated every time the form gets shown. What do you think about my answer? – gonzobrains Aug 15 '13 at 21:45
  • @LarsTech On second thought, I think I know what you mean. In the Shown event I could just force the scroll to occur each time, right? I'm not sure I want that to happen each time it is shown (in case the user moves around the textbox), but that is a viable solution in some cases. – gonzobrains Aug 15 '13 at 21:48
  • Shown event only happens once. – LarsTech Aug 15 '13 at 21:52
  • @LarsTech which event is called every time the form becomes visible to the user? Is it VisibleChanged? – gonzobrains Aug 15 '13 at 21:56
  • 1
    Are you looking for Activated? – LarsTech Aug 15 '13 at 21:58
  • That sounds about right. On other platforms they don't call this event "Activated" but rather something else. Thanks again. – gonzobrains Aug 16 '13 at 15:43

2 Answers2

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();
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • 1
    A cleaner approach to doing intialization tasks when the form is ready: https://stackoverflow.com/a/219155/388994 – blade Nov 17 '18 at 20:03
  • 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. – Caglayan DOKME Feb 13 '20 at 09:16
2

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

txtBox1.AppendText("somthing");
txtBox1.ScrollToEnd();
ashish
  • 319
  • 2
  • 12
  • This doesn't work for me - "ScrollToEnd" is not a method of TextBox. I'm using C#, Winforms and the .NET Framework 4.6. – Matt Jan 22 '21 at 00:41