Detect scroll to bottom in WebBrowser control

﹥>﹥吖頭↗ 提交于 2019-12-06 09:34:56

问题


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;
        }
    }

回答1:


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!");
}



回答2:


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



来源:https://stackoverflow.com/questions/41478520/detect-scroll-to-bottom-in-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!