Tag Archives: EM_SCROLL

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… 🙂