Click here to Skip to main content
Click here to Skip to main content
Go to top

Scrolling a Rich/Textbox Automatically

, 1 Feb 2008
Rate this:
Please Sign up or sign in to vote.
How to scroll a textbox by given pixels

Introduction

I’ve been faced with a problem of automatically scrolling a richtextbox. For example: scrolling one pixel every 40 milliseconds.

Using C# under Visual Studio 2005, I’ve dropped a new richtextbox onto my form and expected to see a method similar to scroll(int pixels) under the richtextbox’s available methods.

Using intellisense, we get the following relevant options: ScrollToCaret().

ScrollCaret() will scroll our textbox to a given text, surely that doesn't satisfy our needs.
Ok, so what else have we got? Surprisingly that is all.

Background

So how do we achieve a smooth auto scrolling effect?
We're going to use the Win32 API SendMessage method to send a scroll message to our textbox asking it to scroll itself in pixels.

In case you are not familiar with messages, I strongly recommend reading the following explanation: message_loop.

So How Does a Scroll Message Look Like?

Using Microsoft Spy ++ we are going to view all scroll messages sent to our textbox.
First, launch your ScrollTextBox application which should be a form with a rich/textbox with some text in it. This will give you a scroller to play with.
Then run Spy++ and click the log message icon (ctrl + m).

Drag and drop the scope onto your textbox, next click the Messages tab and select only the Scrollbar option under the Message groups, Click OK.
A new dialog should appear: “Massages (Window 000206B6) though you will probably get a different window number.

Now scroll your textbox using the up/down arrows, or dragging the scroller and watch Spy logs all scroll messages received by the textbox.

scrollMsg.JPG

A Typical Scroll Message

<0007>000206B6 S WM_VSCROLL nScrollCode:SB_THUMBTRACK nPos:16 hwndScrollBar:null) 
  • <0007> is just the logger line number
  • WM_VSCROLL is the type of the message, in our case a Vertical scroll message
  • SB_THUMBTRACK is the first parameter of the message.
    The first message parameter tells us what type of scroll is performed.
    In the example I have given, I'm dragging the scroller.

A full list of first parameter values is given below:

  • nPos specifies the position to scroll to, It is part of the first parameter and is available only for SB_THUMBPOSITION or SB_THUMBTRACK scrolling.
  • hwndScrollBar is the second parameter of the message and is always null.

Here is a list of low-order words of wParam values to a WM_VSCROLL message:

  • SB_LINEUP
  • SB_LINEDOWN
  • SB_THUMBPOSITION
  • SB_THUMBTRACK
  • SB_TOP
  • SB_BOTTOM
  • SB_ENDSCROLL
  • SB_PAGEDOWN
  • SB_PAGEUP

What our simple application will do is send a WM_VSCROLL message to the textbox using the SB_THUMBTRACK parameter. It will set the high-order word of wParam to the desired position we want the scroller to be at.

Importing user32.dll

In order to send messages, we'll need to import the sendmessage method from the Win32 API:

[DllImport("User32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
        protected static extern IntPtr SendMessage
	(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

Now we can send messages to any handle we get our hands on.

int SB_LINEUP = 0; 
ptrWparam = new IntPtr(SB_LINEUP);
ptrLparam = new IntPtr(0);
SendMessage(txtArea.Handle, WM_VSCROLL, ptrWparam, ptrLparam); 

The above code will scroll txtArea one line up. To scroll a textbox by n pixels value, we'll need to do a little bit more work.

What we need is a way to determine where our scroller is at the moment. Then we could increase its position by a pixel value and all that's left is to send a new SB_THUMBTRACK message with the new position back to the textbox. Using the Win32 API GetScrollInfo method, we can get all kinds of information on a scroller bar:

GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);  
  • Hwnd is an handle to our textbox.
  • fnBar is the size of the structure SCROLLINFO.
  • lpsi is a structure that will hold information about the scroller.

The SCROLLINFO structure:

struct SCROLLINFO
{
    public uint cbSize;
    public uint fMask;
    public int nMin;
    public int nMax;
    public uint nPage;
    public int nPos;
    public int nTrackPos;
} 

For more information about the structure, visit MSDN.

What we'll need is the nPos variable which will hold the current scroller position. So our basic scroll method will do something like this:

// Scrolls a given textbox. handle: an handle to our textbox. pixles: 
// number of pixels to scroll.
void scroll(IntPtr handle, int pixles)
{            
    // Get current scroller position
            
    SCROLLINFO si = new SCROLLINFO();
    si.cbSize = (uint)Marshal.SizeOf(si);
    si.fMask = (uint)ScrollInfoMask.SIF_ALL;
    GetScrollInfo(handle, (int)ScrollBarDirection.SB_VERT, ref si);
            
    // Increase position by pixles
    si.nPos += pixles;
                        
    // Reposition scroller
    SetScrollInfo(handle, (int)ScrollBarDirection.SB_VERT, ref si, true);

    // Send a WM_VSCROLL scroll message using SB_THUMBTRACK as wParam
    // SB_THUMBTRACK: low-order word of wParam, si.nPos high-order word of  wParam

    IntPtr ptrWparam = new IntPtr(SB_THUMBTRACK + 0x10000 * si.nPos);
    IntPtr ptrLparam = new IntPtr(0);
        SendMessage(handle, WM_VSCROLL, ptrWparam, ptrLparam);                
} 

That's it now to get the filling that the textbox is scrolling itself smoothly. Use a timer to call the above method say every 40 milliseconds and increase the scroller position by 1 pixel each time.

Ending

Surprisingly this simple "feature" is not built into the textbox functionality.
I've spent some time looking for a quick solution for my problem, but couldn't find one.
So I've decided to post my solution. I hope it will come in handy, In case you have any questions or comments, please feel free to post or you could email me.

History

  • 1st February, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Lipman Roi
Architect AVG
Israel Israel
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionScroll to Centre PinmemberMember 85806693-Dec-13 5:32 
QuestionSolved: Experimenting changing colors, scroll stops at each last passed line instead of current position when paused? [modified] PinmemberDarkTyranno4-Mar-13 3:25 
SuggestionCaution about using WM_VSCROLL! PinmemberHillBillyBoy20-Feb-13 12:13 
GeneralRe: Caution about using WM_VSCROLL! PinmemberLipman Roi22-Feb-13 1:11 
GeneralRe: Caution about using WM_VSCROLL! Pingroupvlad7812-Mar-13 14:05 
NewsThanks! Changed code for use in own project. PinmemberDarkTyranno12-Feb-13 3:50 
GeneralRe: Thanks! Changed code for use in own project. PinmemberLipman Roi12-Feb-13 8:01 
QuestionThis is works for small number of text. Pingroupvlad7817-Jun-12 14:33 
AnswerRe: This is works for small number of text. PinmemberLipman Roi7-Jun-12 20:10 
Generalmajor help needed! Pinmembervlad78127-Jan-11 13:10 
GeneralRe: major help needed! PinmemberLipman Roi27-Jan-11 22:50 
GeneralRe: major help needed! [modified] Pinmembervlad78128-Jan-11 4:28 
GeneralThank You PinmemberMember 740954115-Nov-10 3:51 
Questionhow do you change direction? Pinmembercheborneck22-Mar-10 0:19 
AnswerRe: how do you change direction? PinmemberLipman Roi22-Mar-10 8:04 
GeneralRe: how do you change direction? Pinmembercheborneck22-Mar-10 9:16 
QuestionPlease help take a look at this issue Pinmemberzystory16-Dec-09 16:12 
AnswerRe: Please help take a look at this issue PinmemberLipman Roi16-Dec-09 19:50 
GeneralRe: Please help take a look at this issue Pinmemberzystory17-Dec-09 19:50 
GeneralRefresh Content PinmemberMember 197994811-Dec-09 1:18 
Generalcorner case Pinmemberspelger30-Sep-09 6:08 
GeneralRe: corner case PinmemberLipman Roi30-Sep-09 6:12 
GeneralRe: corner case Pinmemberspelger30-Sep-09 6:34 
GeneralPerfect! PinmemberVin552-Sep-09 7:15 
GeneralRe: Perfect! PinmemberLipman Roi2-Sep-09 9:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 1 Feb 2008
Article Copyright 2008 by Lipman Roi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid