locked
synconize scrolling of two webbrowser controls RRS feed

  • Question

  • Hi everyone,

     

    Does anyone know of a simple code sample I can look at that would allow me to synconize scrolling of two webbrowser controls in VB? I can see lots of similar questions being asked on web searches but nothing comes up that is sauitable... hope you guys/gals would not mind to give advise.


    Thanks
    Kim

    Thursday, May 19, 2011 9:50 PM

Answers

  • Hi Kim,

    Welcome to the MSDN Forum.

    According to your description, it seemst that there are two web browsers in your form, when scroll one of them, the other should be scrolled at the same time.

    If I have misunderstood anything, please feel free to let me know.

    Please take a look at the following code:

      Dim ScrollTop As Integer
      Dim ScrollLeft As Integer
      Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        AddHandler WebBrowser1.Document.Window.Scroll, AddressOf Scroll2
      End Sub
      Sub Scroll2(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
        Console.WriteLine("web1 fired")
        ScrollTop = WebBrowser1.Document.DomDocument.documentElement.ScrollTop
        ScrollLeft = WebBrowser1.Document.DomDocument.documentElement.ScrollLeft
        Console.WriteLine(ScrollLeft & "  " & ScrollTop)
        WebBrowser2.Document.Window.ScrollTo(ScrollLeft, ScrollTop)
      End Sub
    
      Private Sub WebBrowser2_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted
        AddHandler WebBrowser2.Document.Window.Scroll, AddressOf Scroll1
      End Sub
      Sub Scroll1(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
        Console.WriteLine("web2 fired")
        ' this is the same with WebBrowser1
        ScrollTop = CType(WebBrowser2.Document.DomDocument, mshtml.IHTMLDocument).documentElement.ScrollTop
        ScrollLeft = CType(WebBrowser2.Document.DomDocument, mshtml.IHTMLDocument).documentElement.ScrollLeft
        WebBrowser1.Document.Window.ScrollTo(ScrollLeft, ScrollTop)
      End Sub
    

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Mike Feng Tuesday, May 24, 2011 12:51 PM
    • Marked as answer by Mike Feng Friday, May 27, 2011 10:45 AM
    Tuesday, May 24, 2011 12:51 PM

All replies

  • Kim,

    The webbrowser is reusing Internet Explorer.

    As far as I know is there not any webbrowser which has the possibility you ask.

    That it is asked does not mean that there should be a solution for that.

    1 fool can ask more than 10000 wise can answer.

     

     


    Success
    Cor
    Saturday, May 21, 2011 5:25 AM
  • Hi Kim,

    Welcome to the MSDN Forum.

    According to your description, it seemst that there are two web browsers in your form, when scroll one of them, the other should be scrolled at the same time.

    If I have misunderstood anything, please feel free to let me know.

    Please take a look at the following code:

      Dim ScrollTop As Integer
      Dim ScrollLeft As Integer
      Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        AddHandler WebBrowser1.Document.Window.Scroll, AddressOf Scroll2
      End Sub
      Sub Scroll2(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
        Console.WriteLine("web1 fired")
        ScrollTop = WebBrowser1.Document.DomDocument.documentElement.ScrollTop
        ScrollLeft = WebBrowser1.Document.DomDocument.documentElement.ScrollLeft
        Console.WriteLine(ScrollLeft & "  " & ScrollTop)
        WebBrowser2.Document.Window.ScrollTo(ScrollLeft, ScrollTop)
      End Sub
    
      Private Sub WebBrowser2_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted
        AddHandler WebBrowser2.Document.Window.Scroll, AddressOf Scroll1
      End Sub
      Sub Scroll1(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
        Console.WriteLine("web2 fired")
        ' this is the same with WebBrowser1
        ScrollTop = CType(WebBrowser2.Document.DomDocument, mshtml.IHTMLDocument).documentElement.ScrollTop
        ScrollLeft = CType(WebBrowser2.Document.DomDocument, mshtml.IHTMLDocument).documentElement.ScrollLeft
        WebBrowser1.Document.Window.ScrollTo(ScrollLeft, ScrollTop)
      End Sub
    

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Mike Feng Tuesday, May 24, 2011 12:51 PM
    • Marked as answer by Mike Feng Friday, May 27, 2011 10:45 AM
    Tuesday, May 24, 2011 12:51 PM