Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change scrollbar position in TextBox?

If I want to change the position of a TextBox's scrollbar, what do I need to do besides this:

SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

This function only changes the scrollbar position, but it doesn't update the actual TextBox (so the scrollbar "scrolls", but the text doesn't). Any suggestions? I'm using Windows Forms and .NET 4, with Visual Studio 2008.

like image 660
igal Avatar asked Dec 20 '10 21:12

igal


1 Answers

First, define a constant value:

const int EM_LINESCROLL = 0x00B6;

Then, 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);

You may also use GetScrollPos() for scroll position saving when textbox is updated:

[DllImport("user32.dll")]
static extern int GetScrollPos(IntPtr hWnd, int nBar);
like image 182
THE DOCTOR Avatar answered Sep 17 '22 00:09

THE DOCTOR