'Force scroll to the top of a RichTextBox

I'm using the following code to force the RichTextBox to scroll to the real bottom

Private Const WM_VSCROLL As Integer = 277
    Private Const SB_PAGEBOTTOM As Integer = 7

<DllImport("user32.dll", CharSet:=CharSet.Auto)>
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function
Friend Shared Sub ScrollToBottom(ByVal richTextBox As RichTextBox)
        SendMessage(richTextBox.Handle, WM_VSCROLL, CType(SB_PAGEBOTTOM, IntPtr), IntPtr.Zero)
        richTextBox.SelectionStart = richTextBox.Text.Length
    End Sub
' to call it
ScrollToBottom(RichTextBox1)

Is there a way to reverse this code so I can force it to to always scroll up? I am not looking to append text solutions. Thanks



Solution 1:[1]

This Should do it:

Private Const WM_VSCROLL As Integer = &H115
Private Const SB_TOP As Integer = 6
Private Const SB_BOTTOM As Integer = 7

<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
    ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function

' scroll RichTextBox
Friend Shared Sub ScrollRichTextBox(ByVal richTextBox As RichTextBox, Direction as integer)
    SendMessage(richTextBox.Handle, WM_VSCROLL, Direction, 0)
End Sub

' to call it
ScrollRichTextBox(RichTextBox1, SB_TOP)  'Scrolls to top
ScrollRichTextBox(RichTextBox1, SB_BOTTOM)  'Scrolls to bottom

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1