Now I can synchronize my two RichTextBox using this part of the code:
private const int SB_HORZ = 0x0; private const int SB_VERT = 0x1; private const int WM_HSCROLL = 0x114; private const int WM_VSCROLL = 0x115; private const int SB_THUMBPOSITION = 4; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int GetScrollPos(int hWnd, int nBar); [DllImport("user32.dll")] private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw); [DllImport("user32.dll")] private static extern bool PostMessageA(IntPtr hWnd, int nBar, int wParam, int lParam); internal int HScrollPos { private get { return GetScrollPos((int)this.Handle, SB_HORZ); } set { SetScrollPos((IntPtr)this.Handle, SB_HORZ, value, true); PostMessageA((IntPtr)this.Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0); } } internal int VScrollPos { get { return GetScrollPos((int)this.Handle, SB_VERT); } set { SetScrollPos((IntPtr)this.Handle, SB_VERT, value, true); PostMessageA((IntPtr)this.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0); } }
I can synchronize RichTextBoxes during keystrokes, up and Vscroll events. This is actually not my goal, I want to synchronize my RichTextBoxes based on the content that I need:
- Get the current
RichTextBox line of the RichTextBox form. - Set the scrollbar position using the line number in another
RichTextBox (without losing focus from the current one). - Get line number using scrollbar position.
Note: you can ask if you need more details.
Thanks in advance.
c # winforms richtextbox scrollbar
Badro niaimi
source share