Change scrollbar position in TextBox?

If I want to change the position of the scrollbar TextBox, what do I need to do in addition to this:

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

This function only changes the scrollbar, but does not update the actual one TextBox(so the scrollbar scrolls, but the text is missing). Any suggestions? I am using Windows Forms and .NET 4 with Visual Studio 2008.

+5
source share
3 answers

I usually do:

textBox1.Select(textBox1.Text.Length, 0);
textBox1.ScrollToCaret();

If you select 0 characters, it only moves the cursor to the desired location (in the code example: at the end of the text).

+7
source

First define a constant value:

const int EM_LINESCROLL = 0x00B6;

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

, , :

SetScrollPos(myTextBox.Handle,1,myTextBox.Lines.Length-1,true);
SendMessage(myTextBox.Handle,EM_LINESCROLL,0,
                             myTextBox.Lines.Length-1);

GetScrollPos() :

[DllImport("user32.dll")]
static extern int GetScrollPos(IntPtr hWnd, int nBar);
+4

, . TextBox.SelectionStart, , - , . ScrollToCaret. . .

TextBox - - , 23 , SO, . , 640 , Window 2.0 386UUX . WPF .

+1
source

All Articles