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?

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.