How to create your Own Web Browser Using C#

In this tutorial I’m going to teach how to create your own Web Browser Using C#. This lesson will also involve the use of some very common tools such as Buttons and WebBrowser. To start building this application, let’s create a new project and call it as “WebBrowser” then design it that looks like as shown below. wev1 If you have find difficulties in finding the WebBrowser Tool you can follow it in the image provided below. wev2 Next, on the “Go” button, double click it and add the following code:
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             webBrowser1.Navigate(textBox1.Text );
  4.         }
In the code above, we simply assign a web URL from the textbox to the browser. And it loads the document at the specified Uniform Resource Locator based on the user input. Then we need also to add code for our “Back” button. This back button takes you to the last page stored in the web browser. And here’s the following code.
  1. private void btnBack_Click(object sender, EventArgs e)
  2.         {
  3.             webBrowser1.GoBack();
  4.         }
Next, for the “Forward” button, this button allows you to move to the next page in the navigation history, if one is available. And here’s the following code.
  1. private void btnForward_Click(object sender, EventArgs e)
  2.         {
  3.             webBrowser1.GoForward();
  4.  
  5.         }
And also for the “Refresh” button, here’s the following code:
  1. private void btnRefresh_Click(object sender, EventArgs e)
  2.         {
  3.             webBrowser1.Refresh();
  4.         }
And if you try to run your application, it will give you some error when navigating the web browser. Because the URL in the textbox is not changing, especially when you click the “Back” or the “Forward” button. So to fix this bug, first select the “Web Browser” on the Form then on the “Properties”, choose “events” or the “lightning icon”. And double click “Navigated”. The process is also shown in the figure below. wev3 Then add this following code:
  1.   private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  2.         {
  3.             //gets the URL of the current document and assign to the Textbox
  4.             textBox1.Text = webBrowser1.Url.ToString();
  5.         }
And you can now press “F5” to your application. And it will look like as shown below. wev4 And here’s all the code use for this application.
  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.Windows.Forms;
  9.  
  10. namespace webBrowser
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             webBrowser1.Navigate(textBox1.Text );
  22.         }
  23.  
  24.         private void btnBack_Click(object sender, EventArgs e)
  25.         {
  26.             webBrowser1.GoBack();
  27.         }
  28.  
  29.         private void btnForward_Click(object sender, EventArgs e)
  30.         {
  31.             webBrowser1.GoForward();
  32.  
  33.         }
  34.  
  35.         private void btnRefresh_Click(object sender, EventArgs e)
  36.         {
  37.             webBrowser1.Refresh();
  38.         }
  39.  
  40.         private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  41.         {
  42.             //gets the URL of the current document and assign to the Textbox
  43.             textBox1.Text = webBrowser1.Url.ToString();
  44.         }
  45.  
  46.      
  47.  
  48.        
  49.     }
  50. }

Add new comment