0

I've tried all the answers suggest in Stack Overflow to get my scrollbar to move to the bottom as text is being updated, but I have a feeling its not working because it's within a thread. My code is below ...

foreach(HtmlAgilityPack.HtmlNode paginationUser in paginationUsers) {
                    String userUrl = paginationUser.GetAttributeValue("id","");
                    this.Invoke((MethodInvoker)delegate { 
                        txtLog.AppendText("...... Added " + userUrl + Environment.NewLine);
                        txtLog.Select(txtLog.Text.Length, 0);
                        txtLog.ScrollToCaret();

                    });
                }

Is it the thread thats causing the code not to work? And what's a better solution?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Sian Jakey Ellis
  • 435
  • 1
  • 4
  • 13

3 Answers3

0

I feel using this code is better:

TextBox.AppendText("your text")

it will automatically scroll to the end of the newly appended text & the auto scrolling animation seems more smoother compared to TextBox.ScrollToCaret() method

you can put this code at TextChanged TextBox event

Dennis
  • 3,528
  • 4
  • 28
  • 40
0

I am having the same problem with WPF, using a thread to write to the textbox. It works fine until I add the ScrollToEnd.

I have no solution, just some remarks.

You are not locking the control. You should if you are filling it from a tread. If I use Invoke it does work (but the UI becomes unresponsive). I use BeginInvoke, which is smoother but then it totally locks up if I use ScrollToEnd.

It seems to be some kind of triggering issue, one event causing the other.

Try feeding text slowly and see what happens, the worker thread may be flooding the textbox, giving it a really hard time, not allowing the main thread to do its thing.

Martin Maat
  • 714
  • 4
  • 23
0

Try to add this code :

  TextBox.SelectionStart = txtLogEntries.Text.Length;
  TextBox.ScrollToCaret();

at onTextChanged TextBox event .

Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89