2011-09-26 67 views
0

我需要幫助,讓所有從WebBrowser1的文字,我textbox1.textVB.NET的Webbrowser到文本框

我試圖

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

textbox3是我的網站 和TextBox1中正在被我希望所有的文本。

+2

使用'WebClient'代替。 – SLaks

+0

'。導航**開始**導航,因此您需要在導航完成並且文檔已經加載時提取內容 –

回答

1

您必須處理WebBrowser控件的DocumentCompleted事件。

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

我的解決辦法:

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