15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Displaying output as an HTMl Page ❘ 645<br />

figure 24-4<br />

launching internet explorer instances<br />

It might be that you are not interested in hosting a browser inside your application, as shown in the previous<br />

section, but instead are only interested in allowing the user to find your web site in a typical browser (for<br />

example, by clicking a link inside your application). For an example of this task, create a Windows Forms<br />

application that has a LinkLabel control on it. For instance, you can have a form that has a LinkLabel<br />

control on it that states “Visit our company web site!”<br />

When you have this control in place, use the following code to launch your company’s web site in an<br />

independent browser as opposed to directly being in the form of your application:<br />

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)<br />

{<br />

WebBrowser wb = new WebBrowser();<br />

wb.Navigate("http://www.wrox.com", true);<br />

}<br />

In this example, when the LinkLabel control is clicked by the user, a new instance of the WebBrowser class<br />

is created. Then, using the WebBrowser class’s Navigate() method, the code specifies the location of the web<br />

page as well as a Boolean value that specifies whether this endpoint should be opened within the Windows<br />

Forms application (a false value) or from within an independent browser (using a true value). By default,<br />

this is set to false. With the preceding construct, when the end user clicks the link found in the Windows<br />

application, a browser instance will be instantiated, <strong>and</strong> the Wrox web site at www.wrox.com will be launched.<br />

giving your application more ie-Type features<br />

In the previous example, in which you used the WebBrowser control directly in the Windows Forms application,<br />

you may notice that when you click on the links contained in the page, the text within the TextBox control is<br />

not updated to show the URL of the exact location where you are in the browsing process. You can fix this by<br />

listening for events coming from the WebBrowser control <strong>and</strong> adding h<strong>and</strong>lers to the control.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!