Click here to Skip to main content
Click here to Skip to main content

How to change scrollbars position in a multiline textbox

By , 29 Jun 2005
 

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:

const int EM_LINESCROLL = 0x00B6;

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

[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:

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

About the Author

Carlos Eugênio X. Torres
Web Developer
Portugal Portugal
View my bio at http://www.carloseugeniotorres.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionEM_LINESCROLL what is itmemberPeter Hawke27-Apr-12 13:55 
QuestionHere is the VB versionmemberPeter Hawke27-Apr-12 13:52 
Here is the Visual Basic version for those who like it
 <DllImport("User32.dll">
    Public Shared Function SetScrollPos(ByVal hWnd As IntPtr, nBar As Integer, nPos As Integer, bRedraw As Boolean) As Integer
 
    End Function
    <DllImport("user32.dll")>
    Public Shared Function SendMessage(hWnd As IntPtr, wMsg As Integer, wParam As Integer, lParam As Integer) As Integer
    End Function

GeneralGetScrollPosmemberevilrifleman1-Jul-10 17:16 
QuestionHow to do the Horizontal Scroll?memberchrisliando25-Feb-08 17:49 
AnswerRe: How to do the Horizontal Scroll?memberevilrifleman1-Jul-10 17:20 
QuestionMoving vertical scrollbar to the end of multi-line text box problem???memberbishnupatro11-Feb-08 19:37 
GeneralSimple scrollmemberks_visa29-Nov-06 4:19 
GeneralOnly SendMessage does stuffmemberThomasKaiser11-Nov-05 15:21 
QuestionWhy do it so difficult?sussAnonymous30-Jun-05 16:05 
AnswerRe: Why do it so difficult?memberCarlos Eugênio X. Torres21-Jul-05 13:14 
GeneralRe: Why do it so difficult?memberNadaChaayaTadros2-Jun-06 0:31 
AnswerScrolling Horizontally....memberPradeep babu21-Jul-05 18:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 29 Jun 2005
Article Copyright 2005 by Carlos Eugênio X. Torres
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid