Detect scroll to bottom in WebBrowser control

I am creating a Windows Form to accept some terms and conditions for a company. So the terms and conditions are on the web and it is navigated to the WinForm through WebBrowser control. It is required to enable the Accept button only after the full document is scrolled to the bottom. I am searching for an Event similar to ValueChanged Event in VScrollBar control(mentioned below) or any other option.

private void vScrollBar1_ValueChanged(object sender, EventArgs e)
    {
        if (vScrollBar1.Value+9 == vScrollBar1.Maximum)
        {
            acceptBtn.Enabled = true;
        }
    }

Answers:

Answer

You should handle onscroll event of window object and check if scrollHeight - scrollTop equals to clientHeight for documentElement. To do so:

private void webBrowser1_DocumentCompleted(object sender, 
    WebBrowserDocumentCompletedEventArgs e)
{
    this.webBrowser1.Document.Window.AttachEventHandler("onscroll", OnScroll);
}

void OnScroll(object sender, EventArgs e)
{
    var script =
    @"(function()
       {
           var e = document.documentElement;
           if (e.scrollHeight - e.scrollTop === e.clientHeight)
               return true;
           else
               return false;
       })();";
    var result = webBrowser1.Document.InvokeScript("eval", new object[] { script });
    if ((bool)result)
        MessageBox.Show("Scrolled to end!");
}
Answer

The scrollbar is not part of the WebBrowser control, but of the Html displayed. You have to subscribe to the Scroll event of the Window of the displayed Document

webBrowser1.Document.Window.Scroll += MyScrollCode;

https://msdn.microsoft.com/en-us/library/system.windows.forms.htmlwindow.scroll(v=vs.110).aspx

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.