Discussion:
Getting Cursor to End of a TextBox in Form_Load Fails
(too old to reply)
Freddo
2010-03-05 14:52:38 UTC
Permalink
Hi All,

In the Form_Load event, I'm running the following code after populating a
TextBox from a file. The TextBox's MultiLine property is enabled and I have
a vertical scrollbar on this control. This code DOES work if outside of the
Form_Load event if when it's executed "after" the form is loaded.

My question is this: Is there a Form event I can use or am I missing
something regarding an event in the the control itself I should use? Here's
the code:

' Set the cursor to the end of the textbox.
txtScroll.SelectionStart = txtScroll.TextLength
txtScroll.ScrollToCaret() ' Scroll down to the cursor position.
--
Thanks in advance for your help -- it is greatly appreciated!
Kind regards - Fred
Herfried K. Wagner [MVP]
2010-03-06 01:48:17 UTC
Permalink
Post by Freddo
In the Form_Load event, I'm running the following code after populating
a TextBox from a file. The TextBox's MultiLine property is enabled and I
have a vertical scrollbar on this control. This code DOES work if
outside of the Form_Load event if when it's executed "after" the form is
loaded.
My question is this: Is there a Form event I can use or am I missing
something regarding an event in the the control itself I should use?
' Set the cursor to the end of the textbox.
txtScroll.SelectionStart = txtScroll.TextLength
txtScroll.ScrollToCaret() ' Scroll down to the cursor position.
Did you already try to place the 'ScrollToCaret' call in the form's
'Shown' event?

In addition, take a look at this alternative method:

Scrolling the textbox control to top or bottom
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=textboxscroll&lang=en>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Freddo
2010-03-08 15:18:15 UTC
Permalink
Hi Herfried,

I thought I completely exhausted the list of form event handlers but somehow
overlooked "Form.Shown!" Thanks for pointing this one out to me - it worked
perfectly!

Kind regards - Fred

###
Post by Herfried K. Wagner [MVP]
Post by Freddo
In the Form_Load event, I'm running the following code after populating
a TextBox from a file. The TextBox's MultiLine property is enabled and I
have a vertical scrollbar on this control. This code DOES work if
outside of the Form_Load event if when it's executed "after" the form is
loaded.
My question is this: Is there a Form event I can use or am I missing
something regarding an event in the the control itself I should use?
' Set the cursor to the end of the textbox.
txtScroll.SelectionStart = txtScroll.TextLength
txtScroll.ScrollToCaret() ' Scroll down to the cursor position.
Did you already try to place the 'ScrollToCaret' call in the form's
'Shown' event?
Scrolling the textbox control to top or bottom
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=textboxscroll&lang=en>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Loading...