简体   繁体   中英

Web browser control not working to get text

I'm trying to get source code of a webpage and save it to richtextbox and that web browser control navigates to new URL. But I'm getting a blank rich text box. Here is my code:

private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        webBrowser2.Navigate("http://www.gmail.com");
    }
private void timer1_Tick(object sender, EventArgs e)
    {
        if(webBrowser2.ReadyState == WebBrowserReadyState.Complete)
        {
            timer1.Enabled = false;
            richTextBox1.Text = webBrowser2.DocumentText;
            webBrowser2.Navigate("new URL");
        }
    }

I see two alternatives to what you're doing.

1) Using DocumentCompleted :

private void Form_Load(object sender, EventArgs e) {
    webBrowser.Navigate("www.google.com");
}

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    richTextBox.Text = webBrowser.DocumentText;
}

2) Using the WebClient :

private void Form_Load(object sender, EventArgs e) {
    using (System.Net.WebClient wc = new System.Net.WebClient()) {
        richTextBox.Text = wc.DownloadString("http://www.google.com");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM