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

How to change scrollbars position in a multiline textbox

, 29 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

About the Author

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

Comments and Discussions

 
QuestionEM_LINESCROLL what is it PinmemberPeter Hawke27-Apr-12 14:55 
QuestionHere is the VB version PinmemberPeter Hawke27-Apr-12 14:52 
GeneralGetScrollPos Pinmemberevilrifleman1-Jul-10 18:16 
QuestionHow to do the Horizontal Scroll? Pinmemberchrisliando25-Feb-08 18:49 
AnswerRe: How to do the Horizontal Scroll? Pinmemberevilrifleman1-Jul-10 18:20 
QuestionMoving vertical scrollbar to the end of multi-line text box problem??? Pinmemberbishnupatro11-Feb-08 20:37 
GeneralSimple scroll Pinmemberks_visa29-Nov-06 5:19 
GeneralOnly SendMessage does stuff PinmemberThomasKaiser11-Nov-05 16:21 
QuestionWhy do it so difficult? PinsussAnonymous30-Jun-05 17:05 
AnswerRe: Why do it so difficult? PinmemberCarlos Eugênio X. Torres21-Jul-05 14:14 
Your way actually works, but not with the same effect of mine. Yours makes the cursor in textbox goes to the the last caracter of the text, forcing the vertical scrollbar to go down, nice, but when the textbox has horizontal bar because of the text is not wraped, the effect is bad, because it moves the horizontal bar too.
My way moves only the vertical bar down and puts the cursor at the last line, but not in the last caracter, it's nicer for what I was doing.
But thanks for the comment! Smile | :)
 
Carlos Eugênio Torres
Software Analyst and Developer
Microsoft Certified Application Developer (MCAD.NET)
email: carloseugeniotorres@gmail.com
GeneralRe: Why do it so difficult? PinmemberNadaChaayaTadros2-Jun-06 1:31 
AnswerScrolling Horizontally.... PinmemberPradeep babu21-Jul-05 19:48 

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 | Terms of Use | Mobile
Web04 | 2.8.150301.1 | Last Updated 29 Jun 2005
Article Copyright 2005 by Carlos Eugênio X. Torres
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid