Scroll down to C # TextBox

I have a TextBox in a C # Forms Application. I populate the TextBox with information about the form load event. Then I call the following:

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

However, the TextBox does not scroll to the bottom?

This applies only to the Load event. I also update this TextBox from other parts of the application after it launches, and as soon as one of these events updates the TextBox, it scrolls down.

So, how can I make it scroll the bottom when it pre-populates a TextBox in a Form Load event?

+60
c # winforms textbox
Aug 04 '09 at 16:54
source share
2 answers

Try putting the code in the Form Shown event:

 private void myForm_Shown(object sender, EventArgs e) { txtLogEntries.SelectionStart = txtLogEntries.Text.Length; txtLogEntries.ScrollToCaret(); } 
+92
Aug 04 '09 at 16:57
source share

While the Load event (occurs before the form is displayed) is processed, there is no form to display, and therefore there is no visual state. Therefore, scrolling an invisible control most likely does nothing, because - there is nothing that can be scrolled, since the scrollable viewport is just an idea of ​​the control, but not part of its state.

You may have more success when moving the scrollable part to the Shown event (occurs after the form is displayed) of the form

+16
Aug 04 '09 at 16:59
source share



All Articles