Synchronized scrolling for two Richtextboxes (C #)

I need help. I am currently working on a Script editor in C #, and I have two rich text fields: programTextBox, where is all the text and linesTextBox, which counts and shows the number of lines.

I want them to scroll at the same time. I did some searching, and I really found some kind of code that works, but if there are a few problems. Here is the code:

public enum ScrollBarType : uint { SbHorz = 0, SbVert = 1, SbCtl = 2, SbBoth = 3 } public enum Message : uint { WM_VSCROLL = 0x0115 } public enum ScrollBarCommands : uint { SB_THUMBPOSITION = 4 } [DllImport("User32.dll")] public extern static int GetScrollPos(IntPtr hWnd, int nBar); [DllImport("User32.dll")] public extern static int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); 

...

 private void programTextBox_VScroll(object sender, EventArgs e) { int nPos = GetScrollPos(programTextBox.Handle, (int)ScrollBarType.SbVert); nPos <<= 16; uint wParam = (uint)ScrollBarCommands.SB_THUMBPOSITION | (uint)nPos; SendMessage(linesTextBox.Handle, (int)Message.WM_VSCROLL, new IntPtr(wParam), new IntPtr(0)); } 

It works. View. And you may ask: "What is the problem?" Well:

1) My program crashes when the total number of lines is about 2500. I get an overflow error.

2) If I move up and down using the scroll bar instead of the mouse wheel, then my second rich text box (linesTextBox) will not follow the first unless I let go of the scroll bar.

+5
source share
1 answer

If the application scrolls the contents of the window, it should also reset the scroll box using the SetScrollPos function. Look at this: https://msdn.microsoft.com/en-in/library/aa926329.aspx

Also, the WM_VSCROLL message contains only 16 bits of data for the position of the scroll window, this may be the reason you should try GetScrollInfo because it has 32 bits of data for the posroll box pos. This can solve both of your problems. Hope this helps ...

0
source

All Articles