WebBrowser and HTML capture (ignore previous)

G

Guest

Sorry, I attempted to tab in the previous post and accidentally posted
prematurely.

I am trying to use the WebBrowser control from Visual Studio 2005 to load a
web page and display its HTML content in a text box on a Form of a Windows
Application. I have a button on the Form that invokes the following code
when clicked:

webBrowser1.Url = new Uri("http://www.microsoft.com");
textBox1.Text = webBrowser1.DocumentText;

However, this does not work because the page does not actually load until
the button click method has returned. To actually load and display the page,
I have to click the button twice and do this:

if (toggle == 0)
{
webBrowser1.Url = new Uri("http://www.microsoft.com");
toggle = 1;
}
else
{
textBox1.Text = webBrowser1.DocumentText;
toggle = 0;
}

I want to capture the page contents with a single button click, but I
suspect I will have to do something in a thread or use invoke or something.
Can someone give me an example of how to do this?

Thanks in advance,
Don
 
N

Nicholas Paldino [.NET/C# MVP]

Don,

What I would do is create an event handler for the DocumentCompleted
event, and when that event is fired, set the text to the source. It
wouldn't prevent the button from being clicked twice, but it wouldn't matter
that much, since it would just cause the page to be reloaded, and the event
to be fired again (the state wouldn't be corrupted).

Hope this helps.
 
G

Guest

That was easy and did the trick. Thanks!
Don

Nicholas Paldino said:
Don,

What I would do is create an event handler for the DocumentCompleted
event, and when that event is fired, set the text to the source. It
wouldn't prevent the button from being clicked twice, but it wouldn't matter
that much, since it would just cause the page to be reloaded, and the event
to be fired again (the state wouldn't be corrupted).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Don Tucker said:
Sorry, I attempted to tab in the previous post and accidentally posted
prematurely.

I am trying to use the WebBrowser control from Visual Studio 2005 to load
a
web page and display its HTML content in a text box on a Form of a Windows
Application. I have a button on the Form that invokes the following code
when clicked:

webBrowser1.Url = new Uri("http://www.microsoft.com");
textBox1.Text = webBrowser1.DocumentText;

However, this does not work because the page does not actually load until
the button click method has returned. To actually load and display the
page,
I have to click the button twice and do this:

if (toggle == 0)
{
webBrowser1.Url = new Uri("http://www.microsoft.com");
toggle = 1;
}
else
{
textBox1.Text = webBrowser1.DocumentText;
toggle = 0;
}

I want to capture the page contents with a single button click, but I
suspect I will have to do something in a thread or use invoke or
something.
Can someone give me an example of how to do this?

Thanks in advance,
Don
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top