Create a Web Browser in C#


This article shows you how to easily create a Web Browser with minimal standard functions containing all standard buttons like the Back, Forward Stop and Home buttons. See below image for more details:


Watch code below with step by step explanations:

namespace Web_Browser_CSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

The browser understands Uri as the actual web address of a website. The Load event of the form sets the URL of WebBrowser to the String that is present in the address bar TextBox to create a URI. See below on how to create a Uri object:

        private void Form1_Load(object sender, EventArgs e)
        {
            Uri url = new Uri(txtAddress.Text);
            webBrowser1.Url = url;
        }

The first event that occurs when a new page loads is the Navigating Event. This occurs just before the Web Browser starts loading the next page.




        private void webBrowser1_Navigating(
            object sender, WebBrowserNavigatingEventHandler e)
        {
            lblStatus.Text = "Page Navigating";
        }

The second event fired is the Navigated() Event that acknowledges about the success of Navigating and loading the page. After the Navigated() Event, the Browser continues to load the files of the Web Page.




        private void webBrowser1_Navigated(
            object sender, WebBrowserNavigatedEventHandler e)
        {
            lblStatus.Text = "Page Navigated";
        }

After all the Files in the Web Page have been loaded, the Browser fires the DocumentCompleted() Event. One very important point to notice here: when a document completes loading, its page URL usually differs by some added text. This happens after loading of the page is done. This text in the addressbar is NOT the exact same text that we wrote in the Address bar. Take for example you went to www.ABC.com but after you reached there, you were redirected to www.ABC.com/home/guest. Therefore, we need to continously update the address bar




        private void webBrowser1_DocumentCompleted(
            object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            lblStatus.Text = "Page Loaded Successfully";
            txtAddress.Text = webBrowser1.Url.ToString();
        }

When the Go button is clicked we check for a valid URL supplied and supply it to the Web Browser for Navigation.

        private void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                Uri url = new Uri(txtAddress.Text);
                webBrowser1.Url = url;
            }
            catch (Exception)
            {
                MessageBox.Show("Invalid URL Supplied", "Error");
            }
        }

When the Stop Sign is clicked, the Browser cancels all pending Navigations and stops all stops any Dynamic Page Elements such as sound and animations.

        private void btnStop_Click(object sender, EventArgs e)
        {
            webBrowser1.Stop();
        }

The GoBack() method of the Web Browser does what it reads i.e. it loads the previous page in the Web Browser.

        private void btnBack_Click(object sender, EventArgs e)
        {
            webBrowser1.GoBack();
        }

Similarly the GoForward() method goes one page forward if available.

        private void btnForward_Click(object sender, EventArgs e)
        {
            webBrowser1.GoForward();
        }

When the Home button is clicked, the Web Browser navigates to Home Page of its Current User.

        private void btnGoHome_Click(object sender, EventArgs e)
        {
            webBrowser1.GoHome();
        }


The following code handles the Search feture of your Web Browser. A set of predefined text followed by your search keyword are to be supplied to the Web Browser for Navigation and that's it. I have used the if....else statements to check for the search engine and then set the URL parameter of the Web Browser. 

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (radioGoogle.Checked == true)
            {
                Uri url = new
                    Uri("http://google.com/search?q=" + textSearch.Text);
                webBrowser1.Url = url;
            }

            if (radioBing.Checked == true)
            {
                Uri url = new
                    Uri("http://www.bing.com/search?q=" + textSearch.Text);
                webBrowser1.Url = url;
            }

            if (radioAsk.Checked == true)
            {
                Uri url = new 
                    Uri("http://www.ask.com/web?q=" + textSearch.Text);
                webBrowser1.Url = url;
            }
        }

        private void btnSavePage_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowSaveAsDialog();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPrintDialog();
        }

        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPrintPreviewDialog();
        }

        private void btnPageSettings_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPageSetupDialog();
        }

The code below deals with the showing of another form that shows the source code the current Web Page. We have set the public string value to the source html and then it shows in the next form. When the next form loads, it copies the public string value into its RichTextBox. 


        private void btnSourceCode_Click(object sender, EventArgs e)
        {
            frmSourceCode frm = new frmSourceCode();
            frm.strhtml = webBrowser1.DocumentText;
            frm.Show();
        }

    }
}

Directly Download Source Code in Visual Studio 2012 Here
Custom Search