3

I want to have 2 (rich)texboxes (vc# 2k8) with same scrolling... so when I scroll tb1 the tb2 scrolls to the same position. I use this functions:

[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

[DllImport("user32.dll")]
public static extern int GetScrollPos(IntPtr hwnd, int nBar);

That works fine but

int pos = GetScrollPos(tb1.Handle, 1);
SetScrollPos(tb2.Handle, 1, pos, true);

only sets the scrollbar to the same position but down update the text in there. tb2.Update() oder Refresh won't work...

Please help. Ty

zee
  • 661
  • 1
  • 10
  • 26

1 Answers1

3

I found answer

[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar,
                int nPos, bool bRedraw);

[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);



public void ScrollTo(int Position) {
    SetScrollPos((IntPtr)textBox1.Handle, 0x1, Position, true);
    PostMessage((IntPtr)textBox1.Handle, 0x115, 4 + 0x10000 * Position, 0);
} 
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
dufanAFK
  • 31
  • 2
  • Thank you for this! This is about the only post on Stackoverflow that deals with proper 32bit increment scrolling (a previous bit of code I used was only 16bits of data, and used "WM_VSCROLL"). Do you have any very subtle variations on the above? I ask because although it solved my case, I had to do a bit more work to stop some line flickering.. Btw to GetScrollPos, one can use: public int GetScroll() { return GetScrollPos((IntPtr)textBox1.Handle, (int)ScrollBarType.SbVert); } – Dan W Nov 06 '11 at 01:11