Loading...

How to Make a Web Browser in C# and Storing history in text Step by Step easy Tutorial

Making a Web browser in C#:

Making a web browser in C# Programming seems very difficult and alien to us, and whenever we think to make a web browser in C#, we think that it will be a very hard job that require tons of coding and working day and night for many days, but in real the scenario is completely instead of our thoughts and it is a very easy task that a beginner of C# Programming who have knowledge about Windows Form Application.
Here we are giving Step by Step easy Tutorial to make a web browser in C#.


Starting Making of Web Browser:


  • Add Web Browser from toolbox in Windows Form Application
  • Add submit , home, back , Forward , refresh and stop buttons on Form like given below:

  • Do Coding on the back of Click events of Buttons like shown in Picture:
  • Include namespace "System.IO" to use Filing elements by writing using System.IO;

      public Form1() // Constructor
        {
            InitializeComponent();
        }
        ~Form1() //  Destructor to Close the file
        {
            tw.Close();
        }
        TextWriter tw;
        string address;
        private void btn_submit_Click(object sender, EventArgs e) //click event of submit button
        {         
           
           address = txt_search.Text;// Storing Address from the Search TectBox
            webBrowser1.Navigate(address);
         
              using (tw = new StreamWriter(@"browser.txt", true)) //Filing to store the History, File will be made in Debug Folder
        
            {
                tw.WriteLine(address);// Writing Address on text file
                tw.Close();
            }    

        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) //Navigate Event of Web Browser
        {
            address = webBrowser1.Url.ToString(); 
            this.Text = webBrowser1.DocumentTitle.ToString(); 
            tw.Write(address);
        }

        private void btn_stop_Click(object sender, EventArgs e)  // Stop Button event
        {
            webBrowser1.Stop(); // Built in Method to stop surfing
        }

        private void btn_home_Click(object sender, EventArgs e)  // Home Button event
        {
            webBrowser1.GoHome();// Built in Method to go home
        }

        private void btn_back_Click(object sender, EventArgs e)   // Back Button event
        {
            webBrowser1.GoBack();  // Built in Method to go back
        }

        private void btn_forward_Click(object sender, EventArgs e)// Forward Button event
        {
            webBrowser1.GoForward();// Built in Method to go Forward
        }

        private void button3_Click(object sender, EventArgs e)
        {
            webBrowser1.Refresh();// Built in Method to Refresh Current Page
        }

        private void Form1_Load(object sender, EventArgs e) // Form load event
        {
            btn_back.Enabled = false; // 
            btn_forward.Enabled = false;
            btn_home.Enabled = false;                
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) // Enables the buttons when documet is completed
        {
            if (webBrowser1.CanGoBack == true)
            {
                btn_back.Enabled = true;
            }
            else
            {
                btn_back.Enabled = false;
            }
            if (webBrowser1.CanGoForward == true)
            {
                btn_forward.Enabled = true;
            }
            else
            {
                btn_forward.Enabled = false;
            }
         
        }

        private void historyToolStripMenuItem_Click(object sender, EventArgs e) // to see history
        {
            TextReader tr = new StreamReader(@"browser.txt");
            string histry = tr.ReadToEnd();
            MessageBox.Show(histry,"History"); // I used simple Message Box.. You can use another Options
            tr.Close();
        }


Output:


Thanks For Reading.. Contact us if you have any Problem, We will help you.

Commenting on a post is a great way to show some love for what you're seeing.

Google+

 
TOP