Web Browser in C#


Attached project is a Web Browser application developed using C# 2.0. Download and check it out. Feel free to contact me or post comments about this project.

I have used Windows Forms to develop this application. The Browser control is used to display Web pages and class is WebBrowser.

If you look at the intial code, I create a WebBrowser object and sets its DocumentTitleChanged, StatusTextChanged, and Navigateed events. After that, I call GoHome method.

Rest of the code is written in respective event handlers.

 

public frmmain()
        {
            InitializeComponent();
            pnlsearch.Hide();
            tabControl1.TabPages.Clear();
            Create_New_Tab();
            WebBrowser webpage = GetCurrentWebBrowser();
            webpage.DocumentTitleChanged += new EventHandler(webpage_DocumentTitleChanged);
            webpage.StatusTextChanged += new EventHandler(webpage_StatusTextChanged);
            webpage.Navigated += new WebBrowserNavigatedEventHandler(webpage_Navigated);
            webpage.GoHome();
            toolStripButton2.Visible = false;
        }

Here is the complete code listing.

Form Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml.XPath;
using System.Collections;
using System.Diagnostics;
using System.Drawing.Printing;

namespace wBrowse
{
    public partial class frmmain : Form
    {

//To keep track of or keep count tabs in browser and add new tabs
        ArrayList tabpages = new ArrayList();

//to keep count or keep add new wepages
        ArrayList webpages = new ArrayList();
        //Too keep Current Tab
        int current_tab_count = 0;
        //To Moving of Image
        int image_animation = 0;
        bool FullScreen = false;
        int CommandPanelHeight = 0;
        public frmmain()
        {
            InitializeComponent();
            pnlsearch.Hide();
            tabControl1.TabPages.Clear();
            Create_New_Tab();
            WebBrowser webpage = GetCurrentWebBrowser();
            webpage.DocumentTitleChanged += new EventHandler(webpage_DocumentTitleChanged);
            webpage.StatusTextChanged += new EventHandler(webpage_StatusTextChanged);
            webpage.Navigated += new WebBrowserNavigatedEventHandler(webpage_Navigated);
            webpage.GoHome();
            toolStripButton2.Visible = false;
        }
        private void webpage_DocumentTitleChanged(object sender, EventArgs e)
        {
            WebBrowser webtitle = GetCurrentWebBrowser();
                this.Text = webtitle.DocumentTitle + "-wBrowser";
        }
        private void btnhome_Click(object sender, EventArgs e)
        {
            WebBrowser thiswebpage = GetCurrentWebBrowser();
            if (thiswebpage.CanGoForward)
                thiswebpage.GoForward();
        }
        private void btngo_Click(object sender, EventArgs e)
        {
            string url = ComboAddress.Text;
            if (url == "")
                return;
            WebBrowser thispage = GetCurrentWebBrowser();
            thispage.Navigate(url);
            timer1.Enabled = true;
            lblpageinfo.Text = thispage.StatusText.ToString();
            ComboAddress.Items.Add(url);
        }
        private void btnaddnewtb_Click(object sender, EventArgs e)
        {
            Create_New_Tab();
            WebBrowser thiwebpage = GetCurrentWebBrowser();
            if (this.ComboAddress.Text == "")
                thiwebpage.GoHome();
            else
                thiwebpage.Navigate(ComboAddress.Text);
        }
        private void btndeltab_Click(object sender, EventArgs e)
        {
            if (current_tab_count < 2) return;

            TabPage current_tab = tabControl1.SelectedTab;
            WebBrowser thiswebpage = (WebBrowser)webpages[tabpages.IndexOf(current_tab)];
            thiswebpage.Dispose();
            tabpages.Remove(current_tab);
            current_tab.Dispose();
            tabControl1.TabPages.Remove(current_tab);
            current_tab_count--;
        }
        private void btnback_Click(object sender, EventArgs e)
        {
            WebBrowser thispage = GetCurrentWebBrowser();
            if (thispage.CanGoBack)
                thispage.GoBack();
        }
        private void btnHome_Click_1(object sender, EventArgs e)
        {
            WebBrowser thiswebpage = GetCurrentWebBrowser();
            thiswebpage.GoHome();
            timer1.Enabled = true;
        }
        private void btnrefresh_Click(object sender, EventArgs e)
        {
            WebBrowser thiswebpage = GetCurrentWebBrowser();
            thiswebpage.Refresh();
            timer1.Enabled = true;
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            WebBrowser thiswebpage = GetCurrentWebBrowser();
            thiswebpage.Stop();
        }
        private void btnsearch_Click(object sender, EventArgs e)
        {
            WebBrowser thisweb = GetCurrentWebBrowser();
            thisweb.Url = new Uri("http://search.msn.com/results.aspx?FORM=SMCRT&q=" + txtsearch.Text);
            timer1.Enabled = true;
        }
        private void txtsearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnsearch.PerformClick();
        }
        private void ComboAddress_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btngo.PerformClick();
        }
        private void ComboAddress_SelectedIndexChanged(object sender, EventArgs e)
        {
            btngo.PerformClick();
        }

//function to add new tab in web browser
        private void Create_New_Tab()
        {
            if (current_tab_count == 10) return;

//text tht will appear on first time opening of aplication
            TabPage newpage = new TabPage("Loading...");
            tabpages.Add(newpage);
            tabControl1.TabPages.Add(newpage);
            current_tab_count++;
            WebBrowser webpage = new WebBrowser();
            webpages.Add(webpage);
            webpage.Parent = newpage;
            webpage.Dock = DockStyle.Fill;

//event to keep check of webpage loaded completly or not
            webpage.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webpage_DocumentCompleted);
            timer1.Enabled = true;
            tabControl1.SelectedTab = newpage;
        }

//to get value or got focus on current tab in browser
        private WebBrowser GetCurrentWebBrowser()
        {
            TabPage current_tab = tabControl1.SelectedTab;
            WebBrowser thispasge = (WebBrowser)webpages[tabpages.IndexOf(current_tab)];
            return thispasge;
        }

//update name of tabpages while adding new tabs
        private void UpdateName(TabPage tb)
        {
            TabPage current_tab = tb;
            WebBrowser thiswebpage = (WebBrowser)webpages[tabpages.IndexOf(current_tab)];

            if (thiswebpage.Document != null)
                current_tab.Text = thiswebpage.Document.Title;
            else
                current_tab.Text = "Loading...";
        }

//to update all tab pages accordinlgy opend sites
        private void UpdateAllNames()
        {
            foreach (TabPage tb in tabControl1.TabPages)
            {
                UpdateName(tb);
            }
        }
        void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            UpdateAllNames();
            UpdateBackForwardButtons();
            timer1.Enabled = false;
            pictureBox_busy.Image = wBrowse.Properties.Resources._4;
        }

//function to perform back and forward operation in web browser
        private void UpdateBackForwardButtons()
        {
            WebBrowser thiswebpage = GetCurrentWebBrowser();

            if (thiswebpage.CanGoBack) btnback.Enabled = true;
            else btnback.Enabled = false;

            if (thiswebpage.CanGoForward) btnforward.Enabled = true;
            else btnforward.Enabled = false;

            if (current_tab_count > 1) btndeltab.Enabled = true;
            else btndeltab.Enabled = false;
        }
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            UpdateBackForwardButtons();
        }
        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmabout about = new frmabout();
            about.Show();
        }
        private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Create_New_Tab();
            WebBrowser thiwebpage = GetCurrentWebBrowser();
            if (this.ComboAddress.Text == "")
                thiwebpage.GoHome();
            else
                thiwebpage.Navigate(ComboAddress.Text);
        }
        private void savePageAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser websave = GetCurrentWebBrowser();
            websave.ShowSaveAsDialog();
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            WebBrowser webprop = GetCurrentWebBrowser();
            webprop.ShowPropertiesDialog();
        }
        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser webp_setup = GetCurrentWebBrowser();
            webp_setup.ShowPageSetupDialog();
        }
        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser web_p_preview = GetCurrentWebBrowser();
            web_p_preview.ShowPrintPreviewDialog();
        }
        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser web_print = GetCurrentWebBrowser();
            web_print.ShowPrintDialog();
        }
        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (current_tab_count < 2) return;

            TabPage current_tab = tabControl1.SelectedTab;
            WebBrowser thiswebpage = (WebBrowser)webpages[tabpages.IndexOf(current_tab)];
            thiswebpage.Dispose();
            tabpages.Remove(current_tab);
            current_tab.Dispose();
            tabControl1.TabPages.Remove(current_tab);
            current_tab_count--;
        }
        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser web_open = GetCurrentWebBrowser();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "wBrowser Open file...";
            ofd.AddExtension = true;
            ofd.Filter = "All Files|*.*|HTML Files|*.htm;*.html|Text Files|" +
              "*.txt|GIF Files|*.gif|JPEG Files|*.jpg;*.jpeg|" +
              "PNG Files|*.png|ART Files|*.art|AU Files|*.au|" +
              "AIFF Files|*.aiff;*.aif|XBM Files|*.xbm";
            ofd.FilterIndex = 1;
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                web_open.Navigate(ofd.FileName);
                ComboAddress.Text = ofd.FileName;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            image_animation++;
            if (image_animation > 5) image_animation = 0;
            switch (image_animation)
            {
                case 0: pictureBox_busy.Image = wBrowse.Properties.Resources._1; break;
                case 1: pictureBox_busy.Image = wBrowse.Properties.Resources._2; break;
                case 2: pictureBox_busy.Image = wBrowse.Properties.Resources._3; break;
                case 3: pictureBox_busy.Image = wBrowse.Properties.Resources._4; break;
                case 4: pictureBox_busy.Image = wBrowse.Properties.Resources._5; break;
                case 5: pictureBox_busy.Image = wBrowse.Properties.Resources._6; break;
            }
        }
        private void printToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            WebBrowser toolprint = GetCurrentWebBrowser();
            toolprint.ShowPrintDialog();
        }
        private void printPreviewToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            WebBrowser toolpreview = GetCurrentWebBrowser();
            toolpreview.ShowPrintPreviewDialog();
        }
        private void pageSetupToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            WebBrowser toolsetup = GetCurrentWebBrowser();
            toolsetup.ShowPageSetupDialog();
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void frmmain_FormClosing(object sender, FormClosingEventArgs e)
        {
            string machinename = "";
            machinename = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
            Application.Exit();
            MessageBox.Show("Thanks " + machinename + " to using wBrowser.\nYour feedback is important for me so keep in touch with me.\nMailID:[email protected]", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void contactUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser contact = GetCurrentWebBrowser();
            contact.Navigate("C:/Documents and Settings/Admin/Desktop/wBrowse/wBrowse/index.html");
            timer1.Enabled = true;
        }
        private void btnorkut_Click(object sender, EventArgs e)
        {
            WebBrowser orkt = GetCurrentWebBrowser();
            orkt.Navigate("http://www.orkut.com");
            timer1.Enabled = true;
        }
        private void btngmail_Click(object sender, EventArgs e)
        {
            WebBrowser gmail = GetCurrentWebBrowser();
            gmail.Navigate("http://www.gmail.com");
            timer1.Enabled = true;
        }
        private void btngoogle_Click(object sender, EventArgs e)
        {
            WebBrowser google = GetCurrentWebBrowser();
            google.Navigate("http://www.google.com");
            timer1.Enabled = true;
        }
        private void btnmsn_Click(object sender, EventArgs e)
        {
            WebBrowser msn = GetCurrentWebBrowser();
            msn.Navigate("http://www.msn.com");
            timer1.Enabled = true;
        }
        private void btnyahoo_Click(object sender, EventArgs e)
        {
            WebBrowser yahoo = GetCurrentWebBrowser();
            yahoo.Navigate("http://www.yahoo.com");
            timer1.Enabled = true;
        }
        private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FullScreen = !FullScreen;
            if (FullScreen)
            {
                if (this.WindowState == FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Normal;
                }
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
                menuStrip1.Visible = false;
                statusStrip1.Visible = false;
                statusStrip1.Height = 1;
                toolStripButton2.Visible = true;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.Sizable;
                this.WindowState = FormWindowState.Maximized;
                menuStrip1.Visible = true;
                statusStrip1.Visible = true;
                toolStripButton2.Visible = false;
                statusStrip1.Height = CommandPanelHeight;
            }
        }
        private void windowsMessangerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = "msnmsgr.exe";
            proc.Start();
        }
        private void gtalkMessangerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            try
            {
                p.StartInfo.FileName = "C:/Program Files/Google/Google Talk/googletalk.exe";
                p.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Not able to locate file:\n " + ex.Message);
            }
        }
        private void yahooToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process prc = new Process();
            try
            {
                prc.StartInfo.FileName = "C:/Program Files/Yahoo!/Messenger/YahooMessenger.exe";
                prc.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Not able to locate file: " + ex.Message);
            }
        }
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser webcopy = GetCurrentWebBrowser();
            webcopy.Document.ExecCommand("Copy", false, null);
            timer1.Enabled = true;
            pasteToolStripMenuItem.Enabled = true;
        }
        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser webpaste = GetCurrentWebBrowser();
            webpaste.Document.ExecCommand("Paste", false, null);
            timer1.Enabled = true;
        }
        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser webct = GetCurrentWebBrowser();
            webct.Document.ExecCommand("Cut", false, null);
            timer1.Enabled = true;
        }
        private void frmmain_Load(object sender, EventArgs e)
        {
            pasteToolStripMenuItem.Enabled = false;
        }
        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser selall = GetCurrentWebBrowser();
            selall.Document.ExecCommand("SelectAll", false, null);
            timer1.Enabled = true;
        }
        private void webpage_StatusTextChanged(object sender, EventArgs e)
        {
            WebBrowser webstatus = GetCurrentWebBrowser();
            lblpageinfo.Text = webstatus.StatusText;
            //timer1.Enabled = true;
        }
        private void webpage_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            WebBrowser webnavi = GetCurrentWebBrowser();
            ComboAddress.Text = webnavi.Url.ToString();
            //timer1.Enabled=true;
        }
        private void largestToolStripMenuItem_Click(object sender, EventArgs e)
        { }
        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlsearch.Show();
            btnclose.Focus();
        }
        private void btnfind_Click(object sender, EventArgs e)
        {
        }
        private void btnclose_Click(object sender, EventArgs e)
        {
            pnlsearch.Hide();
        }
        private void btnclose_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                pnlsearch.Hide();
        }
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            btnclose_KeyUp(this, e);
            if (e.KeyCode == Keys.Enter)
                btnfind.PerformClick();
        }
        private void btnfind_KeyUp(object sender, KeyEventArgs e)
        {
            btnclose_KeyUp(this, e);
        }
        private void btnfindnext_KeyUp(object sender, KeyEventArgs e)
        {
            btnclose_KeyUp(this, e);
        }
        private void chkmatch_KeyUp(object sender, KeyEventArgs e)
        {
            btnclose_KeyUp(this, e);
        }

        private void zoomOuToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void clearAddressBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ComboAddress.Items.Clear();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            FullScreen = !FullScreen;
            if (FullScreen)
            {
                if (this.WindowState == FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Normal;
                }
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
                menuStrip1.Visible = false;
                statusStrip1.Visible = false;
                statusStrip1.Height = 1;
                toolStripButton2.Visible = true;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.Sizable;
                this.WindowState = FormWindowState.Maximized;
                menuStrip1.Visible = true;
                statusStrip1.Visible = true;
                toolStripButton2.Visible = false;
                statusStrip1.Height = CommandPanelHeight;
            }
        }

        private void pageSourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void workOfflineToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
    
    }
}