tags:

views:

65

answers:

1

How do you sync scroll two winforms webrowser controls?

Meaning when you scroll one up and down, the other scrolls to the same spot in the underlying document?

+1  A: 

I had this answer ahead of time but had no luck finding the answer via google, so I am posting here for posterity.

In the DocumentCompleted event for the browsers, add a handler to the webbrowser control's Window.Scroll event

webBrowserRight.Document.Window.Scroll += ScrollHandler;

I also set the titles here too (made distinguishing them easier when I needed to later)

webBrowserRight.Document.Title = "right";

Write the handler code - here I only needed to sync scroll vertically, but you could do the horizontal sync too.

private void ScrollHandler( object sender, EventArgs e )
{
    var scrolledBrowser = sender as HtmlWindow;
    if( scrolledBrowser == null ) return;

    // here you can see where I needed to distinguish the browser windows
    // none of the document, window etc properties matched the sender, so I
    // resorted to this hacky way
    WebBrowser otherBrowser = scrolledBrowser.Document.Title == "right" 
    ? webBrowserLeft 
    : webBrowserRight;
    int y = scrolledBrowser.Document.Body.ScrollRectangle.Top;
    otherBrowser.Document.Body.ScrollTop = y;
}
BioBuckyBall