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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With