Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to bottom of C# TextBox [duplicate]

I have a TextBox on a C# Forms Application. I populate the TextBox with information on the Load event of the form. I then call the following:

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

However the TextBox does not scroll to the bottom ?

This only applies to the Load event though. I also update this TextBox from other parts of the application once it's running, and as soon as one of these events update's the TextBox, it is scrolled to the bottom.

So, how can I get it to scroll to the bottom when pre populating the TextBox in the Form Load event?

like image 571
SnAzBaZ Avatar asked Aug 04 '09 16:08

SnAzBaZ


People also ask

How do I scroll to the bottom of an element?

You can use element. scroll() to scroll to the bottom of an element.

How do you scroll to the bottom in CSS?

use css position top keep it at the bottom {position : relative; bottom:0;} .

How do you scroll to the bottom of a page automatically?

To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.


2 Answers

Try putting the code in the Form's Shown event:

private void myForm_Shown(object sender, EventArgs e) {   txtLogEntries.SelectionStart = txtLogEntries.Text.Length;   txtLogEntries.ScrollToCaret(); } 
like image 61
bernhof Avatar answered Oct 21 '22 15:10

bernhof


While the Load event (occurs before the Form is shown) is processed, there is no form to display yet, and thus no visual state has been established. Scrolling a non-visible control therefore very likely doesn't do anything because—hey, there is nothing to scroll as a scrolling viewport is just a view on the control but not part of its state.

You may have more success with moving the scrolling part into the Shown event (occurs after the form is first shown) of the form

like image 21
Joey Avatar answered Oct 21 '22 15:10

Joey