VB.NET - 在 DocumentText 更新后滚动到 WebBrowser 控件的底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14307190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET - Scroll to bottom of WebBrowser Control after DocumentText Update
提问by eqiz
So i have looked at several other posts on stackoverflow and none of them seem to be working for me to accomplish this. All I want to do is for the WebBrowser control to automatically scroll down to the very bottom after I change something programmatically in the webbrowser1.documenttext property.
因此,我查看了有关 stackoverflow 的其他几篇文章,但似乎没有一个适合我来完成此任务。我想要做的就是让 WebBrowser 控件在我以编程方式更改 webbrowser1.documenttext 属性中的某些内容后自动向下滚动到最底部。
I've tried ALL of the following ways, and none of them work... I actually have them literally all in the exact same line of code.
我已经尝试了以下所有方法,但都没有奏效......实际上我将它们全部放在完全相同的代码行中。
WebBrowser1.ScrollBarsEnabled = True
WebBrowser1.Document.Body.ScrollIntoView(False)
WebBrowser1.Document.Window.ScrollTo(New Point(WebBrowser1.Height, WebBrowser1.Height))
WebBrowser1.Document.Window.ScrollTo(WebBrowser1.Height, WebBrowser1.Height)
WebBrowser1.AutoScrollOffset = New Point(WebBrowser1.Height, WebBrowser1.Height)
In my WebBrowser1 control all I have done is set started it with and then at the end placed and in the middle all i've done is copied 'n pasted the following...
在我的 WebBrowser1 控件中,我所做的一切都是设置启动它,然后在最后放置并在中间我所做的所有操作都是复制并粘贴以下内容...
<html><body>
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
<div style="color: red;">blah blah</div><Br />
</body></html>
about 30 times... BUT I can't get anything to work. None of the code above is doing anything.
大约 30 次……但我什么也做不了。上面的代码都没有做任何事情。
What am I missing?
我错过了什么?
VS 2005 SP1 - VB.NET
VS 2005 SP1 - VB.NET
回答by John Koerner
Your problem may be you are trying to access the DOM before it is updated. Fire your code to scroll the correct element into view in the DocumentCompleted event, like this:
您的问题可能是您尝试在 DOM 更新之前访问它。触发您的代码以将正确的元素滚动到 DocumentCompleted 事件中的视图中,如下所示:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.DocumentText = <html><body>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;'>blah blah</div><Br/>
<div style='color: red;' id="lastElement">blah blah</div><Br/>
</body></html>.ToString()
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim lastGuy= WebBrowser1.Document.GetElementById("lastElement")
If lastGuy<> Nothing Then
lastGuy.ScrollIntoView(True)
End If
End Sub
回答by anonymous
wb1.Navigate("javascript:window.scroll(0,document.body.scrollHeight);")

