1

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");
        }
    }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

2

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");
    }
}
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • Wihitehead i have tried the 1st method but richtextbox is still blank and new URL also not taking? –  Feb 04 '13 at 10:09
  • 1
    You need to do a little bit of work for yourself.. I omitted various things like subscribing to the event, navigating afterwards.. etc. You need to fill in the blanks that are relevant to you. – Simon Whitehead Feb 04 '13 at 10:10