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 ❘ 651<br />

Printing using the Webbrowser Control<br />

Not only can users use the WebBrowser control to view pages <strong>and</strong> documents, but they can also use the<br />

control to send these pages <strong>and</strong> documents to the printer for printing. To print the page or document being<br />

viewed in the control, simply use the following construct:<br />

webBrowser1.Print();<br />

As before, you do not need to view the page or document to print it. For instance, you can use the<br />

WebBrowser class to load an HTML document <strong>and</strong> print it without even displaying the loaded document.<br />

This can be accomplished as shown here:<br />

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

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

wb.Print();<br />

displaying the Code of a requested Page<br />

In the beginning of this chapter, you used the WebRequest <strong>and</strong> the Stream classes to get at a remote page to<br />

display the code of the requested page. You used this code to accomplish this task:<br />

public Form1()<br />

{<br />

InitializeComponent();<br />

System.Net.WebClient Client = new WebClient();<br />

Stream strm = Client.OpenRead("http://www.reuters.com");<br />

StreamReader sr = new StreamReader(strm);<br />

string line;<br />

}<br />

while ( (line=sr.ReadLine()) != null )<br />

{<br />

listBox1.Items.Add(line);<br />

}<br />

strm.Close();<br />

Now, however, with the introduction of the WebBrowser control, it is quite easy to accomplish the<br />

same results. To accomplish this, change the browser application that you have been working on thus far in<br />

this chapter. To make this change, simply add a single line to the Document_Completed event, as<br />

illustrated here:<br />

private void webBrowser1_DocumentCompleted(object sender,<br />

WebBrowserDocumentCompletedEventArgs e)<br />

{<br />

buttonStop.Enabled = false;<br />

textBox2.Text = webBrowser1.DocumentText;<br />

}<br />

In the application itself, add another TextBox control below the WebBrowser control. The idea is<br />

that when the end user requests a page, you display not only the visual aspect of the page but also<br />

the code for the page, in the TextBox control. The code of the page is displayed simply by using the<br />

DocumentText property of the WebBrowser control, which will give you the entire page’s content as<br />

a String. The other option is to get the contents of the page as a Stream using the DocumentStream<br />

property. The end result of adding the second TextBox to display the contents of the page as a String is<br />

shown in Figure 24-8.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!