Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on 

.ScrollToCaret() on a Textbox

I have a VB 2005 Windows Form that has a Textbox. I add lines to that textbox as I load the Form. When the Form loads my cursor is at the bottom of the textbox, where it added the last line, which makes sense, but I'd like it to return to the top line so that it can show the list from top to bottom.

I tried tbxMyTexbox.ScrollToCaret(), but I think I'm missing something. Any suggestions what the "Caret" should be?

Thanks,
Fulano
Visual Basic.NET

Avatar of undefined
Last Comment
Mr_Fulano
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
You should use
textbox1.Select(0,0)
 
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

Actually, I tried both approaches and neither worked...

This is what I have...I have two textboxes to which I add one line of text each time I loop through a series of strings. As I add the lines of text the vertical scroll bar begins to scroll and the items that were added initially begin to disappear to the top of the box. What I need to do is return the cursor to the top of the textbox, which I'm assuming will force the textbox to scroll to the top to position (0,0).

Do I have to set any of the textbox's properties to something special?

Thanks,
Fulano
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

Time out... I now see what I did wrong!

I needed to add .ScrollToCaret() to the code as well. Once I did that, both options worked.

Jaime, why is the .Select(0,0) a better option than the .SelectionStart = 0?

Thanks,
Fulano
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

OK, the jury is in...

I tested both solutions with my code. One worked every time, one failed on a couple of occasions.

This worked each time without fail:
tbxMyTextBox.SelectionStart = 0
tbxMyTextBox.ScrollToCaret()

This worked, but failed a on a few occasions:
tbxMyTextBox.Select(0,0)
tbxMyTextBox.ScrollToCaret()

Thanks to both of you for your suggestions.

Fulano
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Sorry Mr_Fulano I had to leave :(

Glad I could help!
Avatar of Mr_Fulano
Mr_Fulano
Flag of United States of America image

ASKER

You did great JPaulino. As always, thanks for your help!

Fulano
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo