How to scroll to the end of System.Windows.Forms.WebBrowser?

How can you programmatically scroll to the end of System.Windows.Forms.WebBrowser?

+5
source share
7 answers
ctlWebBrowser.Document.Body.ScrollIntoView(false);

The boolean parameter for ScrollIntoView () is true to align the scroll bar at the top of the document and false to align the scroll bar at the bottom of the document.

MSDN documentation here: HtmlElement.ScrollIntoView

+14
source

I set the property DocumentTextof the control WebBrowser(with html and body tags), and the method Document.Body.ScrollIntoView(false)did not work for me, but it works:

    private void ScrollToBottom()
    {
        // MOST IMP : processes all windows messages queue
        Application.DoEvents();

        if (webBrowser1.Document != null)
        {
            webBrowser1.Document.Window.ScrollTo(0, webBrowser1.Document.Body.ScrollRectangle.Height);
        }
    }

: http://kiranpatils.wordpress.com/2010/07/19/webbrowsercontrol-scroll-to-bottom/

+12

When I did not have the final element of the body, this worked for me (VB.NET):

WebBrowser1.Document.Body.All(WebBrowser1.Document.Body.All.Count - 1).ScrollIntoView(False)
+1
source
wb1.Navigate("javascript:window.scroll(0,document.body.scrollHeight);")
+1
source

Addendum to user2349661 Answer that this is the same for C #:

WebBrowser1.Document.Body.All[WebBrowser1.Document.Body.All.Count -1].ScrollIntoView(False)

nb would add as a comment, but I don't have enough points!

+1
source

Using javascript creates security issues

webBrowser.Navigate("javascript:window.scroll(...);")

Better to use a direct call like

webBrowser.Document.Window.ScrollTo(...)
+1
source

Inside the document, a Completed Event would be a good option:

private void Form1_Load(object sender, EventArgs e)
{

webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;
webBrowser1.Navigate("http://stackoverflow.com/questions/990651/how-to-scroll-to-end-of-system-windows-forms-webbrowser");

}


private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{

WebBrowser browser = sender as WebBrowser;

browser.Document.Body.ScrollIntoView(false);

}
0
source

All Articles