Today we will focus on creating our own web browser.
C# Web Browser actually provides an Internet Explorer control. The control has several properties, methods, and events that we can use to implement user interface features.
To know more click on ,
Let’s have it practically:
First of all we have to create a new project in VS-2015(me using), this time we will click on Windows Form Application in template window.
Name it as your wish, I named it Web Browser. Finally the sample application window will appear with a blank sample form.
For our Navigation bar we will now drag n drop the ToolStrip menu to the project window and create our navigation button as we need. doc it to top.
Now we will add some icon to our application resource and give the button a look.
To show the status we need to add StatusStrip from Toolbar’s section Menus & Tools, drag n drop it on application form and doc it to bottom.
Now we will drag n drop the Web Browser control from the toolbar. We will doc it as Fill, so that the websites can view with fullscreen.
Ok now let’s add some mechanism with this form controll and view the website by user request.
FORM1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Web_Browser { public partial class Form1 : Form { String Url = string.Empty; public Form1() { InitializeComponent(); Url = "http://www.msn.com"; myBrowser(); } private void Form1_Load(object sender, EventArgs e) { toolStripButton1.Enabled = false; toolStripButton2.Enabled = false; } private void toolStripButton3_Click(object sender, EventArgs e) { myBrowser(); } private void myBrowser() { if (toolStripComboBox1.Text != "") Url = toolStripComboBox1.Text; webBrowser1.Navigate(Url); webBrowser1.ProgressChanged += new WebBrowserProgressChangedEventHandler(webpage_ProgressChanged); webBrowser1.DocumentTitleChanged += new EventHandler(webpage_DocumentTitleChanged); webBrowser1.StatusTextChanged += new EventHandler(webpage_StatusTextChanged); webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webpage_Navigated); webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webpage_DocumentCompleted); } private void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser1.CanGoBack) toolStripButton1.Enabled = true; else toolStripButton1.Enabled = false; if (webBrowser1.CanGoForward) toolStripButton2.Enabled = true; else toolStripButton2.Enabled = false; toolStripStatusLabel1.Text = "Done"; } private void webpage_DocumentTitleChanged(object sender, EventArgs e) { this.Text = webBrowser1.DocumentTitle.ToString(); } private void webpage_StatusTextChanged(object sender, EventArgs e) { toolStripStatusLabel1.Text = webBrowser1.StatusText; } private void webpage_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) { toolStripProgressBar1.Maximum = (int)e.MaximumProgress; toolStripProgressBar1.Value = ((int)e.CurrentProgress < 0 || (int)e.MaximumProgress < (int)e.CurrentProgress) ? (int)e.MaximumProgress : (int)e.CurrentProgress; } private void webpage_Navigated(object sender, WebBrowserNavigatedEventArgs e) { toolStripComboBox1.Text = webBrowser1.Url.ToString(); } private void toolStripButton4_Click(object sender, EventArgs e) { webBrowser1.Refresh(); } private void toolStripButton2_Click(object sender, EventArgs e) { webBrowser1.GoForward(); } private void toolStripButton1_Click(object sender, EventArgs e) { webBrowser1.GoBack(); } private void toolStripButton5_Click(object sender, EventArgs e) { webBrowser1.GoHome(); } private void toolStripButton6_Click(object sender, EventArgs e) { webBrowser1.ShowPrintPreviewDialog(); } } }
Here I set the default url to http://www.msn.com
The out:
Deployment:
At this stage we will create a setup file with InstallShield 2015 Limited. Let’s take another new project like below picture
Click ok and the default screen will appear with several setup information.
Include if any required software needed to run this application.
Add project output
Create shortcut to desktop or start menu with adding icon
add license file
Finally build the project
Install and check it out
after finishing the installation process the icon will appear in program menu and on desktop.
Thanks and hope it will help someone to build own web browser in their application 🙂
Source Code: I’ve uploaded the full source code to download/clone .
Rosalinda says:
Great article.
Shashangka Shekhar says:
Thanks 🙂