Click here to Skip to main content
Licence 
First Posted 29 Jun 2005
Views 61,866
Bookmarked 14 times

How to change scrollbars position in a multiline textbox

By | 29 Jun 2005 | Article
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

About the Author

Carlos Eugênio X. Torres

Web Developer

Portugal Portugal

Member

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionEM_LINESCROLL what is it PinmemberPeter Hawke13:55 27 Apr '12  
QuestionHere is the VB version PinmemberPeter Hawke13:52 27 Apr '12  
GeneralGetScrollPos Pinmemberevilrifleman17:16 1 Jul '10  
QuestionHow to do the Horizontal Scroll? Pinmemberchrisliando17:49 25 Feb '08  
AnswerRe: How to do the Horizontal Scroll? Pinmemberevilrifleman17:20 1 Jul '10  
QuestionMoving vertical scrollbar to the end of multi-line text box problem??? Pinmemberbishnupatro19:37 11 Feb '08  
GeneralSimple scroll Pinmemberks_visa4:19 29 Nov '06  
GeneralOnly SendMessage does stuff PinmemberThomasKaiser15:21 11 Nov '05  
QuestionWhy do it so difficult? PinsussAnonymous16:05 30 Jun '05  
AnswerRe: Why do it so difficult? PinmemberCarlos Eugênio X. Torres13:14 21 Jul '05  
GeneralRe: Why do it so difficult? PinmemberNadaChaayaTadros0:31 2 Jun '06  
AnswerScrolling Horizontally.... PinmemberPradeep babu18:48 21 Jul '05  

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.

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