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

Posted on 2003-03-23
Medium Priority
Last Modified: 2010-04-03
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?
Question by:AnderstheCyborg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8192455
JUST what programming language are you talkin here

Author Comment

ID: 8196426
Sorry for my question's inaccuracy.
The language I am using is Visual Basic 6.0
LVL 24

Expert Comment

ID: 8197089
er, what's this have to do with storage?
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.


Accepted Solution

msa2003 earned 300 total points
ID: 8197949
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 http://www.geocities.com/nicom_2000/VB_API.html 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 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vb98/html/vbprohwnd.asp) 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.

Author Comment

ID: 8198436
Thanks alot!

That should do the trick.
I am proud to reward you the points, Msa2003 :)

Author Comment

ID: 8198441
Just what I needed.
Thanks lad

Expert Comment

ID: 8200660
I'm glad to help you.


Author Comment

ID: 8206360
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?

Expert Comment

ID: 8213517
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

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Concerto Cloud Services, a provider of fully managed private, public and hybrid cloud solutions, announced today it was named to the 20 Coolest Cloud Infrastructure Vendors Of The 2017 Cloud  (http://www.concertocloud.com/about/in-the-news/2017/02/0…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question