Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#
Article

How to change scrollbars position in a multiline textbox

Rate me:
Please Sign up or sign in to vote.
2.52/5 (14 votes)
29 Jun 2005 105.5K   15   12
A way to control a textbox's scrollbars position.

Introduction

I have discovered a nice way to control multi-line textbox's scrollbars in C#. Here we can force the textbox's scrolls down to the end of the text, when we are showing the log file contents dynamically.

The code

First, we have to define a constant value:

C#
const int EM_LINESCROLL = 0x00B6;

Then, we have to declare two external methods of user32.dll:

C#
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, 
                               int nPos, bool bRedraw);
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int wMsg, 
                               int wParam, int lParam);

Finally, use these methods to do the real thing:

C#
SetScrollPos(myTextBox.Handle,1,myTextBox.Lines.Length-1,true);
SendMessage(myTextBox.Handle,EM_LINESCROLL,0,
                             myTextBox.Lines.Length-1);

Done! Simple and easy!

License

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


Written By
Web Developer
Portugal Portugal
View my bio at http://www.carloseugeniotorres.com

Comments and Discussions

 
GeneralOnly SendMessage does stuff Pin
ThomasKaiser11-Nov-05 15:21
ThomasKaiser11-Nov-05 15:21 

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.