set value richtextbox scrollbar

Jordan Halgunseth 1 Reputation point
2021-08-30T16:12:22.94+00:00

I want to set richtextbox scrollbar in middle of box.
How can this be done?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,481 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-08-31T02:12:42.477+00:00

    You can use SetScrollPos to set the scrollbar position, and then use PostMessage to adjust the text to the corresponding position.

            public Form1()  
            {  
                InitializeComponent();  
            }  
            private const int SB_VERT = 0x1;  
      
            [DllImport("user32.dll")]  
            public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);  
      
            [DllImport("User32.Dll", EntryPoint = "PostMessageA")]  
            static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);  
            private void button1_Click(object sender, EventArgs e)  
            {  
                SetScrollPos(richTextBox1.Handle, SB_VERT, 100, true);  
                PostMessage((IntPtr)richTextBox1.Handle, 0x115, 4 + 0x10000 * 100, 0);  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.