开发者

VB.NET Webbrowser to textbox

I need help getting all of the text from WebBrowser1 to my textbox1.text

I tried

    WebBrowser1.Navigate(TextBox3.Text)
    TextBox1.Text = WebBrowser1.DocumentText

textbox3 being my website and textbox1 being were i want all the tex开发者_如何学Pythont.


You have to handle the DocumentCompleted event of WebBrowser control.

Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, 
       e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) 
              Handles WebBrowser1.DocumentCompleted
  TextBox1.Text = WebBrowser1.DocumentText 
End Sub


My solution:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' WebBrowser1
        ' TextBox1
        ' TextBox2
        '
        WebBrowser1.ScriptErrorsSuppressed = True      ' we would like to suppress scripts error message
        WebBrowser1.Navigate("http://codeguru.com")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        ' 1) Get entire html code and save as .html file
        TextBox1.Text = WebBrowser1.DocumentText

        ' HOWTO retry while error UNTIL ok
        ' this needs to be done because Body.InnerText returns error when called too soon
        ' 2) Get Body text and save as .txt file
        Dim retry As Boolean = True
        Dim body As String = ""
        While retry
            Try
                body = WebBrowser1.Document.Body.InnerText
                retry = False
            Catch ex As System.IO.IOException
                retry = True
            Finally
                TextBox2.Text = body
            End Try
        End While

    End Sub
End Class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜