The C# Web Browser controls enable us to host documents in our Windows Forms pages and other Web browsers. In our C# projects we can add browser control fromvisual studio tool box and show web pages like regular commercial web browsers. We can use the browser control to provide our application's integrated HTML-based user support or web browsing skills.
In other words, The WebBrowser control offers a managed wrapper for the ActiveX control of WebBrowser. We can show webpages in our Windows Form customer applications with the controlled wrapper. The web browser control can be used to duplicate the Internet Explorer web browsing feature in our application, and the Internet Explorer functionality can be disabled by default and used as a simple HTML document viewer. We can also use the control to incorporate elements of the DHTML user interface into our form, which can hide the fact they are hosted in our WebBrowser controller.
This approach allows you to seamlessly combine web controls in a single application with the Windows Forms controls.
webBrowser1.Navigate(new Uri(url));
The control of the Web browser has several navigational properties, methods and events.
Go to Home
webBrowser1.GoHome();
Next
webBrowser1.GoForward();
Go Back
webBrowser1.GoBack();
Refresh
webBrowser1.Refresh();
In order to display the SaveAs diaog, Print Dialog, PrintPreview Dialog and the Properties dialogue, the Method ShowSaveAsDialog
, ShowPrintDialog
, ShowPrintPreviewDialog
, and ShowProperties
are used.
Save
webBrowser1.ShowSaveAsDialog();
Print Preview
webBrowser1.ShowPrintPreviewDialog();
webBrowser1.ShowPrintDialog();
Properties
webBrowser1.ShowPropertiesDialog();
The following C# program shows a browser window web page and can also retrieve the same web page source code by clicking on another button.
private void btnViewSource_Click(object sender, EventArgs e)
{
String source = ("ViewSource.txt");
StreamWriter writer = File.CreateText(source);
writer.Write(webBrowser1.DocumentText);
writer.Close();
Process.Start("notepad.exe", source);
}
Web Browser Control Source Code