I'm in need of some help. Currently I am working on a Script Editor in C# and I have two rich text boxes: programTextBox, where the whole text is and linesTextBox which counts and shows the number of lines.
I want them to scroll at the same time. I have done some search and I actually found some code which works, but if has 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. Kind of. And you may ask: "What's the problem?". Well:
1) My program crashes when the total number of lines becomes about 2500. I get an overflow error.
2) If I move up and down by using the scrollbar instead of the mouse wheel, then my second rich text box (linesTextBox) will not follow the first one unless I release the scrollbar.