Click here to Skip to main content
12,554,623 members (55,018 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked

Controlling scrolling with the API

, 4 Feb 2004
Rate this:
Please Sign up or sign in to vote.
Scrolling with the API.


In certain circumstances, we need to be able to control the scroll bars from our code. This may seem like it should be easily done with standard methods when in fact they are buried in the API. I will tell you how I solved this problem in the application I am working on.


You have to import the System.Runtime.InteropServices.Marshal namespace so if you get an error you can access GetLastWin32Error, a method in this namespace. So, let's begin with the import directive.

Imports System.Runtime.InteropServices.Marshal


Along with many API functions, there are a slew of structures, types and constants that are at your disposal. For this, I am only going to use a few constants (we really don't need the rest). However, if you are wondering what the other scrollbar constants are, take a look at WinUser.h (located in the .NET SDK directory tree). Here are the constants I will be using:

' Scrollbar direction
Const SBS_HORZ = 0
Const SBS_VERT = 1

' Windows Messages
Const WM_VSCROLL = &H115
Const WM_HSCROLL = &H114

Declaring the functions

Now comes the (not so) fun part of declaring the functions. The first function we are going to be using is called GetScrollPos (located in afxwin.h). We send this function a handle and a direction. It sends back an integer of the current scroll position.

Private Declare Function GetScrollPos Lib "user32.dll" ( _
        ByVal hWnd As IntPtr, _
        ByVal nBar As Integer) As Integer

  'Example: position = GetScrollPos(textbox1.handle, SBS_HORZ)

The next function is called SetScrollPos, we send SetScrollPos the handle, direction, value and a flag of whether or not it should redraw the control.

 Private Declare Function SetScrollPos Lib "user32.dll" ( _
         ByVal hWnd As IntPtr, _
         ByVal nBar As Integer, _
         ByVal nPos As Integer, _
         ByVal bRedraw As Boolean) As Integer

'Example: SetScrollPos(hWnd, SBS_HORZ, position, True

The last function we will be using is PostMessageA. PostMessageA sends instructions to the control about what to do next. Normally, you don't have to worry about this as it is handled for you. We send this function a handle, command, parameters and the hWnd of the scroll bar (optional).

Private Declare Function PostMessageA Lib "user32.dll" ( _
        ByVal hwnd As IntPtr, _
        ByVal wMsg As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As Integer) As Boolean

'Example: PostMessageA(hWnd, WM_HSCROLL, SB_THUMBPOSITION _
'                         + &H10000 * position, Nothing)


Suppose you have a control called textbox1 on your form and you want to scroll it to line 100. You can do this easily using SetScrollPos and PostMessageA. Here is an example of how this can be accomplished:

If (SetScrollPos(hWnd, SBS_HORZ, position, True) <> -1) Then
                               &H10000 * position, Nothing)
    MsgBox("Can't set info (Err: " & GetLastWin32Error() & ")")
End If

Remember our imports directive, this is where GetLastWin32Error is located. GetLastWin32Error will return a numeric error code if there is a problem. You can look these error codes up in the WinError.h file.

Now, suppose you want to scroll down a few lines but you don't know where exactly the line you need to scroll to. You can use all three of these functions to accomplish this task:

Dim Position = GetScrollPos(hWnd, SBS_VERT) + 20

If (SetScrollPos(hWnd, SBS_VERT, position, True) <> -1) Then
                               &H10000 * position, Nothing)
    MsgBox("Can't set info (Err: " & GetLastWin32Error() & ")")
End If

Well, that's about it for scrolling with API.

I hope you found this article informative. I will include a sample application that I wrote as I was exploring these functions.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Matthew Hazlett
Web Developer
United States United States
I started programming for fun when I was about 10 on an Franklin Ace 1000.

I still do it just for fun but it has gotten me a few jobs over the years. More then I can say for my Microsoft Certifications. Smile | :)

The way I learned was by example, now its time to give back to the next generation of coders.

You may also be interested in...


Comments and Discussions

GeneralControlling scrolling with the API Pin
Member 1074591715-Mar-15 8:28
memberMember 1074591715-Mar-15 8:28 
QuestionWebbrowser Pin
hackerspk30-Aug-12 21:12
memberhackerspk30-Aug-12 21:12 
QuestionScrolling and the ListView Control Pin
Ken Kutcher18-Mar-09 8:19
memberKen Kutcher18-Mar-09 8:19 
QuestionChange scrollbar color in a panel (C#, windows appln.) Pin
NiyaHere29-Aug-08 3:00
memberNiyaHere29-Aug-08 3:00 
AnswerExcellent Pin
Andy Davies16-Jun-08 0:00
memberAndy Davies16-Jun-08 0:00 
GeneralDataGrid Pin
TigerNige25-Oct-05 5:25
memberTigerNige25-Oct-05 5:25 
GeneralRe: DataGrid Pin
axinte200517-Jul-07 22:24
memberaxinte200517-Jul-07 22:24 
Generalcouple of changes I might suggest: Pin
frumbert19-Jul-05 20:49
memberfrumbert19-Jul-05 20:49 
GeneralMSFlexgrid and SetScrollPos Pin
Anonymous14-Apr-05 2:18
sussAnonymous14-Apr-05 2:18 
GeneralOverflow errors Pin
kmsheff25-Nov-04 13:24
memberkmsheff25-Nov-04 13:24 
GeneralGetScrollRange Pin
jnghh11-Mar-04 15:52
memberjnghh11-Mar-04 15:52 
GeneralRe: GetScrollRange Pin
Aaron Eldreth9-May-04 13:08
memberAaron Eldreth9-May-04 13:08 
GeneralRe: GetScrollRange Pin
SnowCarve24-Aug-05 8:47
sussSnowCarve24-Aug-05 8:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 5 Feb 2004
Article Copyright 2004 by Matthew Hazlett
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid