How can I get and set the scrollbar position of a textbox?

As the title says, I am wondering about how to get and set scrollbar positions of a rich textbox control.

I have created an ini programmer, which colors specific codelines.
The whole content of the textbox is refreshed upon repainting, and the selstart is set to what it was before the painting procedure started, but the file is not scrolled down to exactly where it was. It simply use the selstart as the bottom line.

Does anyone know a method to get and set textbox scrollbar positions?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

msa2003Connect With a Mentor Commented:
I have no VB and I could not provide the exact solution but I could describe the steps what you need to perform to get results:

There are two Win32 API function that could be used for your purposes:

int SetScrollPos(

    HWND hWnd,     // handle of window with scroll bar
    int nBar,     // scroll bar flag
    int nPos,     // new position of scroll box
    BOOL bRedraw      // redraw flag

int GetScrollPos(

    HWND hWnd,     // handle of window with scroll bar
    int nBar      // scroll bar flags

SetScrollPos function is used to set the scroll bar position of a control specified by it's handle (hWnd). nPos - scrollbar position (in pixels), bRedrew is set to "TRUE" when the scroll bar needs to be redrawn. nBar could be set to one of the values:

SB_CTL - Sets the position of the scroll box in a scroll bar control. The hWnd parameter must be the handle of the scroll bar control.

SB_HORZ - Sets the position of the scroll box in a window's standard horizontal scroll bar.

SB_VERT - Sets the position of the scroll box in a window's standard vertical scroll bar.

GetScrollPos returns the current scrollbar position of the specified control (parameters means the same).

Refer to to get the instruction how to implement the API call in VB (you need two public declarations that could be generated automatically by the API Text Viewer from MS VB Tools. Use TextBox hWnd property ( to get a handle of the text box control.

constant values:

SB_HORZ = 0 - horizontal scrollbar
SB_VERT = 1 - vertical scrollbar


pos := GetScrollPos(RichEdit1.hWnd, SB_VERT);

gets vertical scrollbar position of RichEdit1 text box;

SetScrollPos(RichEdit1.hWnd, SB_VERT, pos, true);

sets vertical scrollbar position of RichEdit1 text box.
JUST what programming language are you talkin here
AnderstheCyborgAuthor Commented:
Sorry for my question's inaccuracy.
The language I am using is Visual Basic 6.0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

er, what's this have to do with storage?
AnderstheCyborgAuthor Commented:
Thanks alot!

That should do the trick.
I am proud to reward you the points, Msa2003 :)
AnderstheCyborgAuthor Commented:
Just what I needed.
Thanks lad
I'm glad to help you.

AnderstheCyborgAuthor Commented:
Your solution was good and easy to understand, but the text in the textwindow doesn't scroll when the scrollbar is updated.

If you click on the scrollbar however, the text is "updated", but this is not the meaning.

Does anyone have any solutions to this?
Sorry, but I'm afraid that "anyone" means "msa2003" in this topic area).

You might use the following construction to set the text box position instead of only scroll bar position:

SendMessage(RichEdit1.hWnd, WM_VSCROLL, SB_THUMBPOSITION + Pos * 65536, 0);

constant values:


use WM_VSCROLL to scroll vertically and WM_HSCROLL (276) to scroll horisontally.

SendMessage() is an API function (needed to use API Text Viewer) which is used to send a message to the controll (window). The construction prior sends the text box message to scroll to absolute position defined by Pos variable. The Pos value might be retrieved by GetScrollPos function. If you use the prior construction you do not need to use SetScrollPos function.

Sorry, I am not a VB programmer.

Best regards
All Courses

From novice to tech pro — start learning today.