Textbox Scroll to Bottom or Top – Part 2 using VB 6.0 and VB.NET

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


I have a previous post on how to scroll the textbox controls contents to the bottom and the top that many visitors didn’t appear to like. It has a 1.5 rating out of 10 so, it is not liked very much. I really don’t know why because the code seems to work fine. Maybe because the cursor position would be moved? This new code won’t move the actual cursor if thats the case. But anyways, I decided to post another way to scroll the textbox contents by using the Windows API. This method uses the SendMEssage function and passes a couple constants to perform the up and down scrolling process. Below are the codes for VB 6.0 and VB.NET.

_____________________________________

Visual Basic 6.0 Declarations…

    '
    'Used to Send Messages to the control and will be used with a request for the current line number in the textbox control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    '
    'Constants used for the SendMessage API call function.
    '
    Private Const EM_SCROLL = &HB5
    Private Const SB_BOTTOM = 7
    Private Const SB_TOP = 6

————————————————

   

   

Visual Basic .NET Declarations…

    '
    'Used to Send Messages to the control and will be used with a request for the current line number in the textbox control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As _Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    '
    'Constants used for the SendMessage API call function.
    '
    Private Const EM_SCROLL = &HB5
    Private Const SB_BOTTOM = 7
    Private Const SB_TOP = 6

————————————-

Now all thats left is to setup and call the SendMessage API function…

The first parameter of the SendMessage call is the handle to your textbox control that you want to use.

The second parameter is to let the api know we want to perform some type of scroll action on the control.

The third parameter is the type of scroll to perform on the control.

The fourth parameter is Not used in this example.

————————————-

Visual Basic 5.0, 6.0 – Scroll the Textbox to the Bottom/Top…

        '
        'This will make the textbox scroll to the bottom without moving the cusor position.
        SendMessage TextBox1.hWnd, EM_SCROLL, SB_BOTTOM, 0
        '
        'The will make the textbox scroll to the top without moving the cursor.
        SendMessage TextBox1.hWnd, EM_SCROLL, SB_TOP, 0

Visual Basic.NET – Scroll the Textbox to the Bottom/Top…

 

        '
        'This will make the textbox scroll to the bottom without moving the cusor.
        SendMessage(TextBox1.Handle.ToInt32, EM_SCROLL, SB_BOTTOM, 0)
        '
        'The will make the textbox scroll to the top without moving the cursor.
        SendMessage(TextBox1.Handle.ToInt32, EM_SCROLL, SB_TOP, 0)

 

_____________________________________

Alright, those codes above should have moved your textbox control contents down to the bottom and up to the top. Also the cursors position will Not be moved like the other method would do. Hopefully this post will be liked alittle better. If not, then I don’t know what else you want and give up… 🙂

Jason

8 thoughts on “Textbox Scroll to Bottom or Top – Part 2 using VB 6.0 and VB.NET

  1. Indira

    Hi I am restricting my text box to maximum length of some characters. But i dont know hoe to move cursor to starting position again. How to move cursor to starting position in text box in visual basic 6.0?

    Reply
  2. Jason Post author

    Hi, a simple way to cause the textbox to scroll to where the cursor is at in VB 5.0 or 6.0 is to use the code below…


    Text1.SelStart = Text1.SelStart


    Hopefully that is what you want. 🙂

    Jason

    Reply
  3. M Taylor

    Thanks! Just the ticket. Here is what I did with it…

    Public Sub rtf_scroll_to_bottom(ByRef rtf As RichTextBox)
    ‘SELECT NOTHING
    rtf.SelectionStart = rtf.Rtf.Length
    rtf.SelectionLength = 0
    ‘rtf.ScrollToCaret()
    ‘SCROLL TO BOTTOM
    sub_scroll_window(rtf.Handle, +1)
    End Sub
    Private Declare Function SendMessage Lib “user32.dll” Alias “SendMessageA” (ByVal winHandle As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    Public Sub sub_scroll_window(ByVal handle As Int32, ByVal direction As Integer)
    Const EM_SCROLL = &HB5
    Const SB_BOTTOM = 7
    Const SB_TOP = 6

    Select Case direction
    Case -1
    SendMessage(handle, EM_SCROLL, SB_TOP, Nothing)
    Case +1
    SendMessage(handle, EM_SCROLL, SB_BOTTOM, Nothing)
    End Select
    End Sub

    Reply
  4. Hello

    Thank you,
    However, there are better ways i thank you for your code, if only i had not lost my 1 line code i was using

    Reply

Leave a Reply