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.
source share