5

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
OC_
  • 446
  • 6
  • 19

1 Answers1

0

If an application scrolls the content of the window, it must also reset the position of the scroll box by using the SetScrollPos function Have a look at this : https://msdn.microsoft.com/en-in/library/aa926329.aspx

Also WM_VSCROLL message carries only 16 bits of scroll box position data this could be the reason u should try GetScrollInfo because it has 32 bits of scroll box pos data. This might give solution to both of your problems . Hope this helps...

Ryu
  • 191
  • 10