Create Your Own Web Browser In C#

The 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.

Let’s have it practical terms

Firstly, we have to create a new project in VS 2015. Click on Windows Form Application in Templates window.

Windows Form Application

I named it Web Browser. Finally, the sample application window will appear with a blank sample form.

Web Browser

For our Navigation bar we will now drag & drop the ToolStrip menu to the project window and create our navigation button as we need. Dock it to the top.

navigation button

Now we need to add some icon to our Resources folder and give the button a look.

Resource folder

To show the status we need to add StatusStrip from Toolbar section Menus & Toolbars. Drag & drop it on application form and dock it to the bottom.

application form

Now we will drag & drop the Web Browser control from the toolbar. We will doc it as Fill, so that the websites can view with fullscreen.

Fill

Now, let’s add some mechanism with this form control and view the website by user request.

Form1.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. namespace Web_Browser  
  11. {  
  12.     public partialclassForm1: Form  
  13.     {  
  14.         String Url = string.Empty;  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.             Url = "http://www.msn.com";  
  19.             myBrowser();  
  20.         }  
  21.         privatevoid Form1_Load(object sender, EventArgs e)  
  22.         {  
  23.             toolStripButton1.Enabled = false;  
  24.             toolStripButton2.Enabled = false;  
  25.         }  
  26.         privatevoid toolStripButton3_Click(object sender, EventArgs e)  
  27.         {  
  28.             myBrowser();  
  29.         }  
  30.         privatevoidmyBrowser()  
  31.         {  
  32.             if (toolStripComboBox1.Text != "") Url = toolStripComboBox1.Text;  
  33.             webBrowser1.Navigate(Url);  
  34.             webBrowser1.ProgressChanged += newWebBrowserProgressChangedEventHandler(webpage_ProgressChanged);  
  35.             webBrowser1.DocumentTitleChanged += newEventHandler(webpage_DocumentTitleChanged);  
  36.             webBrowser1.StatusTextChanged += newEventHandler(webpage_StatusTextChanged);  
  37.             webBrowser1.Navigated += newWebBrowserNavigatedEventHandler(webpage_Navigated);  
  38.             webBrowser1.DocumentCompleted += newWebBrowserDocumentCompletedEventHandler(webpage_DocumentCompleted);  
  39.         }  
  40.         private void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)  
  41.         {  
  42.             if (webBrowser1.CanGoBack) toolStripButton1.Enabled = true;  
  43.             else toolStripButton1.Enabled = false;  
  44.             if (webBrowser1.CanGoForward) toolStripButton2.Enabled = true;  
  45.             else toolStripButton2.Enabled = false;  
  46.             toolStripStatusLabel1.Text = "Done";  
  47.         }  
  48.         private void webpage_DocumentTitleChanged(object sender, EventArgs e)  
  49.         {  
  50.             this.Text = webBrowser1.DocumentTitle.ToString();  
  51.         }  
  52.         private void webpage_StatusTextChanged(object sender, EventArgs e)  
  53.         {  
  54.             toolStripStatusLabel1.Text = webBrowser1.StatusText;  
  55.         }  
  56.         private void webpage_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)  
  57.         {  
  58.             toolStripProgressBar1.Maximum = (int) e.MaximumProgress;  
  59.             toolStripProgressBar1.Value = ((int) e.CurrentProgress < 0 || (int) e.MaximumProgress < (int) e.CurrentProgress) ? (int) e.MaximumProgress : (int) e.CurrentProgress;  
  60.         }  
  61.         private void webpage_Navigated(object sender, WebBrowserNavigatedEventArgs e)  
  62.         {  
  63.             toolStripComboBox1.Text = webBrowser1.Url.ToString();  
  64.         }  
  65.         private void toolStripButton4_Click(object sender, EventArgs e)  
  66.         {  
  67.             webBrowser1.Refresh();  
  68.         }  
  69.         private void toolStripButton2_Click(object sender, EventArgs e)  
  70.         {  
  71.             webBrowser1.GoForward();  
  72.         }  
  73.         private void toolStripButton1_Click(object sender, EventArgs e)  
  74.         {  
  75.             webBrowser1.GoBack();  
  76.         }  
  77.         private void toolStripButton5_Click(object sender, EventArgs e)  
  78.         {  
  79.             webBrowser1.GoHome();  
  80.         }  
  81.         private void toolStripButton6_Click(object sender, EventArgs e)  
  82.         {  
  83.             webBrowser1.ShowPrintPreviewDialog();  
  84.         }  
  85.     }  
  86. }  
Here I have set the default url.

Output

Output

Deployment

At this stage we will create a setup file with InstallShield 2015 Limited. Let’s take another new project like the following screenshot.

another new project

Click OK and the default screen will appear with setup information.

default screen

Follow the steps one by one and fill in the information as in the following screenshots:

step one by one

information like below images

Add project output.

information

fill-up the information

 Create shortcut on desktop or start menu with adding icon.

Click New

adding icon

Create shortcut

Add license file.

Add license file

Finally build the project.

build the project

Finally build the project

Install and check it out.

Install and check it out

Click next

Click install

Clicl finish

After finishing the installation process the icon will appear in program menu and on the desktop.

 finishing the installation process

desktop

To know more click on Web Browser Capabilities to a Windows Forms Application, WebBrowser Class.

Thanks and I hope it will help someone to build their own Web browser in their application.


Similar Articles