In my WinForm application, I have a multi-line TextBox control (uiResults) that is used to report the progress of processing a large number of elements. Using AppendText is great for automatically scrolling from the bottom with every update, but if the user scrolls back to read some old data, I need to disable autoscrolling. I would prefer to avoid P / Invoke calls if possible.
Is it possible to determine if a user scrolls without using P / Invoke? For now, I'm just checking SelectionStart, which works, but requires the user to move the caret from the end of the text box to stop auto-scrolling:
if(uiResults.SelectionStart == uiResults.Text.Length) { uiResults.AppendText(result + Environment.NewLine); }
My main problem is that when adding a line using the Text property, the text field scrolls to the beginning. I tried to solve this problem by preserving the carriage position and resetting it and scrolling it after updating, but this leads to the current line moving to the bottom (of course, since ScrollToCaret scrolls no more than the required distance to display the carriage).
[Continued from above] else { int pos = uiResults.SelectionStart; int len = uiResults.SelectionLength; uiResults.Text += result + Environment.NewLine; uiResults.SelectionStart = pos; uiResults.SelectionLength = len; uiResults.ScrollToCaret(); }
source share