Click here to Skip to main content
15,879,474 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.7K   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

 
QuestionEM_LINESCROLL what is it Pin
Peter Hawke27-Apr-12 13:55
Peter Hawke27-Apr-12 13:55 
QuestionHere is the VB version Pin
Peter Hawke27-Apr-12 13:52
Peter Hawke27-Apr-12 13:52 
GeneralGetScrollPos Pin
evilrifleman1-Jul-10 17:16
evilrifleman1-Jul-10 17:16 
QuestionHow to do the Horizontal Scroll? Pin
chrisliando25-Feb-08 17:49
chrisliando25-Feb-08 17:49 
AnswerRe: How to do the Horizontal Scroll? Pin
evilrifleman1-Jul-10 17:20
evilrifleman1-Jul-10 17:20 
QuestionMoving vertical scrollbar to the end of multi-line text box problem??? Pin
bishnupatro11-Feb-08 19:37
bishnupatro11-Feb-08 19:37 
GeneralSimple scroll Pin
zeltera29-Nov-06 4:19
zeltera29-Nov-06 4:19 
GeneralOnly SendMessage does stuff Pin
ThomasKaiser11-Nov-05 15:21
ThomasKaiser11-Nov-05 15:21 
QuestionWhy do it so difficult? Pin
Anonymous30-Jun-05 16:05
Anonymous30-Jun-05 16:05 
If all you want to do is move the scroll bar to the bottom of a multiline textbox why not do the following?

this.textBox1.SelectionStart = this.textBox1.TextLength - 1;
this.textBox1.ScrollToCaret();

Einar
AnswerRe: Why do it so difficult? Pin
Carlos Eugênio X. Torres21-Jul-05 13:14
Carlos Eugênio X. Torres21-Jul-05 13:14 
GeneralRe: Why do it so difficult? Pin
NadaChaayaTadros2-Jun-06 0:31
NadaChaayaTadros2-Jun-06 0:31 
AnswerScrolling Horizontally.... Pin
Pradeep babu21-Jul-05 18:48
Pradeep babu21-Jul-05 18:48 

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.