15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

646 ❘ ChaPTer 24 netwOrkinG<br />

Updating the form’s title with the title of the HTML page is easy. You just need to use the Navigated event<br />

<strong>and</strong> update the Text property of the form:<br />

private void webBrowser1_Navigated(object sender, EventArgs e)<br />

{<br />

this.Text = webBrowser1.DocumentTitle.ToString();<br />

}<br />

In this case, when the WebBrowser control moves onto another page, the Navigated event will fire, <strong>and</strong> this<br />

will cause the form’s title to change to the title of the page being viewed. In some instances when working<br />

with pages on the Web, even though you have typed in a specific address, you are going to be redirected to<br />

another page altogether. You are most likely going to want to reflect this in the textbox (address bar) of the<br />

form; to do this, you change the form’s text box based on the complete URL of the page being viewed. To<br />

accomplish this task, you can use the WebBrowser control’s Navigated event as well:<br />

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)<br />

{<br />

textBox1.Text = webBrowser1.Url.ToString();<br />

this.Text = webBrowser1.DocumentTitle.ToString();<br />

}<br />

In this case, when the requested page has finished downloading in the WebBrowser control, the Navigated<br />

event is fired. In your case, you simply update the Text value of the textBox1 control to the URL of the<br />

page. This means that after a page is loaded in the WebBrowser control’s HTML container, <strong>and</strong> if the URL<br />

changes in this process (for instance, if there is a redirect), then the new URL will be shown in the text box.<br />

If you employ these steps <strong>and</strong> navigate to the Wrox web site (www.wrox.com), then you will notice that the<br />

page’s URL will immediately change to www.wrox.com/WileyCDA/. This process also means that if the end<br />

user clicks one of the links contained within the HTML view, then the URL of the newly requested page<br />

will also be shown in the text box.<br />

Now if you run the application with the preceding changes in place, you will find that the form’s title <strong>and</strong><br />

address bar work as they do in Microsoft’s Internet Explorer, as demonstrated in Figure 24-5.<br />

figure 24-5<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!