问题
How can you scroll to the end of a System.Windows.Forms.WebBrowser programmatically?
回答1:
ctlWebBrowser.Document.Body.ScrollIntoView(false);
The boolean parameter for ScrollIntoView() is true to align the scrollbar with the top of the document, and false to align the scrollbar with the bottom of the document.
MSDN documentation here: HtmlElement.ScrollIntoView
回答2:
I'm setting DocumentText property of the WebBrowser control (with html and body tags) and Document.Body.ScrollIntoView(false) method didn't worked for me, but this is working:
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);
}
}
source: http://kiranpatils.wordpress.com/2010/07/19/webbrowsercontrol-scroll-to-bottom/
回答3:
When I had no ending body element, this worked for me (VB.NET):
WebBrowser1.Document.Body.All(WebBrowser1.Document.Body.All.Count - 1).ScrollIntoView(False)
回答4:
wb1.Navigate("javascript:window.scroll(0,document.body.scrollHeight);")
回答5:
Adding to user2349661's answer this is the same for C#:
WebBrowser1.Document.Body.All[WebBrowser1.Document.Body.All.Count -1].ScrollIntoView(False)
n.b. would have added as a comment but I don't have enough points!
回答6:
Using javascript creates ie security problems
webBrowser.Navigate("javascript:window.scroll(...);")
It is better to use a direct call like
webBrowser.Document.Window.ScrollTo(...)
回答7:
Inside a Document 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);
}
来源:https://stackoverflow.com/questions/990651/how-to-scroll-to-end-of-system-windows-forms-webbrowser