Create a Web Browser in C# VS-2015

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.

3

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

4

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.

6

Now we will add some icon to our application resource and give the button a look.

8

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

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.

11

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:

15

Deployment:

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

Click ok and the default screen will appear with several setup information.18

19

Include if any required software needed to run this application.

20

Add project output

21 22

Create shortcut to desktop or start menu with adding icon

23 24 25

add license file

26

Finally build the project

28 29

Install and check it out

30 31 32 33

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

34

35

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 .

Author:

Since March 2011, have 8+ years of professional experience on software development, currently working as Senior Software Engineer at s3 Innovate Pte Ltd.

2 thoughts on “Create a Web Browser in C# VS-2015”

Leave a Reply