Textbox Scroll with Cursor to the Top or Bottom with VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 3.36 out of 5)
 

I originally made this post for VB 6.0, but I figured I would go ahead and add the .NET code here as well. The first codes is for VB 6.0 and the code after is for Visual Basic.NET , VB 2005/2008, VB 2010, and newer.


Visual Basic 6.0

This will scroll to the bottom of the textbox control.

'
Text1.SelStart = Len(Text1.Text)

 

The code below will put the cursor to the beginning of the textbox and scroll to its position.

'
'Goto the top/first character in the textbox control.
TextBox1.SelStart = 0

Visual Basic.NET 2002/2003, and VB.NET 2005, 2008, 2010

This will put the cursor position at the bottom and scroll the textbox to the end of its contents.

'
'Set the cursor to the end of the textbox.
Textbox1.SelectionStart = Textbox1.TextLength
'
'Scroll down to the cursor position.
Textbox1.ScrollToCaret()

 

The codes below will set the cursor and scroll the textbox to the top or beginning of its contents.

'
'Set the cursor to the first character in the textbox which will be at the top of the control.
TextBox1.SelectionStart = 0
'
'Make the textbox scroll to the actually caret postition.
TextBox1.ScrollToCaret()

Minor Revision: 2015

11 thoughts on “Textbox Scroll with Cursor to the Top or Bottom with VB and VB.NET

  1. Jonty

    How do I write code that positions the cursor at the beginning of the selected text?

    Let’s say the textbox contains T characters. I want to select the characters starting at S and for a length of L. S>0, S+L<T. I want the cursor at S, not at S+L.

    textbox1.SelectionStart = 0
    textbox1.SelectionLength = 0

    positions the cursor at the start of the box, just like your examples show.

    textbox1.SelectionStart = S
    texttox1.SelectionLength = L

    selects the characters I want but it leaves the cursor at the end of the selected text, at position S+L.

    textbox1.SelectionStart = S+L
    textbox1.SelectionLength = -L

    selects the characters but, again, it leaves the cursor at the end of the selected text, at position S+L, not at S.

    Any ideas?

    Reply
  2. Jason Post author

    Hi, If I understand what your wanting, then try these codes below. They will select a range of text but leave the cursor at the beginning of the selection.

    Dim s As Integer = 1
    Dim l As Integer = 5

    txtControl.Select(s, l)

    MsgBox(txtControl.SelectionStart)

    See if that does what you want. 🙂

    Jason

    Reply
  3. Yogesh Sinare

    It worked for me!!
    used
    textbox1.SelectionStart = textbox1.TextLength

    textbox1.ScrollToCaret()

    in textbo1_change() in VS2010

    Reply
  4. Sean Harrison

    Annoying. The ScrollToCaret() bit hasn’t worked in several versions, and it doesn’t work in 2010. Please stop posting it.

    Reply
  5. Travis

    I made a simple subroutine that allows you to scroll to the bottom, then use this whenever I update the text box, it works great!

    so if the next time tbstatus gets updated just add the subrouting into it .

    tbstatus.text = “fjdksal;fjsaka;”
    AutoScrollTry()

    Private Sub AutoScrollTry()
    tbStatus.SelectionStart = tbStatus.Text.Length – 1
    tbStatus.ScrollToCaret()
    tbStatus.Refresh()

    End Sub

    Reply

Leave a Reply

Your email address will not be published.