SEARCH C# PROGRAMMING CODES HERE

WebBrowser in C# Visual Studio

WebBrowser 



In this Tutorial, I am going to show you how you can create a web browser in C# Visual Studio. This project is divided into 3-Step so Simply follow the steps one by one as following. So let's Start the Tutorial.

Step-1- To create a new project in c# Visual Studio

Open Visual Studio then go to the top menu bar, Choose File > New > Project > Choose Visual C# then click on Windows Desktop then select WindowsForm App(.Net Framework) and

Name it as web browser then press ok.


Once the New project created go to the Form1 properties and set the properties as follows.



Step-2-web browser Designing
Now we need some Required Components for Designing web browser.

1-Adding Panel

Now add a panel from the toolbox and set the properties as follows.



2-Adding Button
Now add 8 buttons from Toolbox and then go to the properties section and set the properties as follows.


Setting all the button properties.


3-Adding TextBox
Now add 2 text boxes from Toolbox and set the properties as follows.

After setting textboxes properties lets arrange the buttons and textboxes as following.

Now check all the buttons and text boxes are in the right place or not if yes then move to the next step.
4- Adding a buttons background image.

To add image into buttons go to the properties section click on a background image
Now select the Project Resources file and then click on the import button to add an image from your computer.


After adding image its look like this.

5-Add WebBrowser component

Now go to the Toolbox and add a web browser component and set the properties as follows.



6-Adding label

Now add 2 labels from Toolbox and set the properties as follows.

Step-3-Now we will move to the coding part.


Now go to the form1 design view and double click on the Back button.

After clicking on the back button it will take to the btn_click event now copy the following Red highlighted code.


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


Now go to the form1 design view and double click on the Home button.


After clicking on the home button it will take to the btn_home_click event now copy the following Red highlighted code.

 private void btn_home_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("www.google.com");
        }

Now go to the form1 design view and double click on the Forward button.


After clicking on Forwarding button it will take to the btn_forward_click event now copy the following Red highlighted code.

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


Now go to the form1 design view and double click on the Refresh button.


After clicking on the Refresh button it will take to the btn_refresh_click event now copy the following Red highlighted code.

private void btn_refresh_Click(object sender, EventArgs e)
        {
            webBrowser1.Refresh();
        }

Now go to the form1 design view and double click on the Go button.


After clicking on Go button it will take to the btn_go_click event now copy the following Red highlighted code.

 private void btn_go_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www." + txt_go.Text + ".com");
        }

Now go to the form1 design view and double click on the Search button.


After clicking on Search button it will take to the btn_search_click event now copy the following Red highlighted code.


private void btn_search_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www." + txt_search.Text + ".com");
        }

Now Run the program and check all the Buttons are working correctly or not make sure your Internet connection is active.


Now we will add a function to get URL path on the textbox-2 which name is txt_go 


when the user search any keyword on the Search Textbox-2 which is txt_search


by clicking search button which name is btn_search.



Now go to the webBrowser1 Events section double click on DocumentCompleted event then copy the Red highlighted code as following.


 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {
           txt_go.Text = webBrowser1.Url.ToString();

        } 

                                                        OutPut



Now we will add progress bar control which will show the page loading status
So go to the Toolbox and add 1 progress bar as follows.



Now go to webbrowser1 Event Section and double Click on the ProgressChanged event and copy the Red highlighted code as following.


private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)

        {
            try
            {
                progressBar1.Value = Convert.ToInt32(e.CurrentProgress);
                progressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
            }
            catch
            {

            }
        }

Output

Now add a Tabcontrol from Toolbox.

To create functionality to store browsing History and Favorite keyword Into the list box, so add 2 list boxes, 4 buttons from Toolbox and set the properties as follows.

tabPage1 settings


Now go to the webBrowser1 Events section double click on DocumentCompleted event then copy the Red highlighted code as following.


private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
           History.Items.Add(txt_go.Text);

        }

 Now go to the Form1_Load Event and copy the Red highlighted code as following.

 private void Form1_Load(object sender, EventArgs e)
        {
         tabControl1.Hide();

        }

Now double click on the Settings button which name is btn_1 to show History and Favorite list.


Now copy the Red highlighted code as following.

private void btn_settings_Click(object sender, EventArgs e)
        {
            tabControl1.Show();
        }



Now double click on the clear button which name is btn_1 to clear all the History items.
Now copy the Red highlighted code as following.

private void btn_1_Click(object sender, EventArgs e)
        {
            History.Items.Clear();
        }

Now double click on Hide button which name is btn_2, now copy the Red highlighted code as following.


 private void btn_2_Click(object sender, EventArgs e)
        {    
 tabControl1.Hide();
 }


Now select tabcontrol1 and click on tabPage2, now drag and drop the listbox2, button3 and button4 as following.



Now go to the properties section and select TabPages and then click on (collection) to open Tab page Collection Editor Once it will open then go to the tabPage2 properties and name the Text as Favorite, Now go to the listbox2 properties and name it as Favorite, similarly for button3 and button4 set the properties as shown in the figure.



Now double click on Favorite button which name is btn_favorite to clear all the History items.


Now copy the Red highlighted code as following.

 private void btn_favorite_Click(object sender, EventArgs e)
        {
            Favorite.Items.Add(txt_go.Text);
        }


Now double click on the clear button which name is btn_3 to clear all the History items.
Now copy the Red highlighted code as following.

private void btn_3_Click(object sender, EventArgs e)
        {
            History.Items.Clear();


        }

Now double click on Hide button which name is btn_4, now copy the Red highlighted code as following.


 private void btn_4_Click(object sender, EventArgs e)
        {
            tabControl1.Hide();
        }

So finally we are done now run the program and check its working correctly or not.

If any error occurs then just Recheck all the source codes as following.

Full source code


using System;
using System.Windows.Forms;

namespace Web_Browser_1._0_By_Rohit_Programming_Zone
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

 private void btn_home_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("www.google.com");
        }

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

private void btn_refresh_Click(object sender, EventArgs e)
        {
            webBrowser1.Refresh();
        }

 private void btn_go_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www." + txt_go.Text + ".com");
        }

private void btn_search_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www." + txt_search.Text + ".com");
        }

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
           txt_go.Text = webBrowser1.Url.ToString();
        }


 private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
       
{
            try
            {
                progressBar1.Value = Convert.ToInt32(e.CurrentProgress);
                progressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
            }
            catch
            {

            }
        }

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
           History.Items.Add(txt_go.Text);

        }

 private void Form1_Load(object sender, EventArgs e)
        {
         tabControl1.Hide();

        }


private void btn_settings_Click(object sender, EventArgs e)
        {
            tabControl1.Show();
        }


private void btn_1_Click(object sender, EventArgs e)
        {
            History.Items.Clear();
        }


 private void btn_2_Click(object sender, EventArgs e)
        {
            tabControl1.Hide();
        }


 private void btn_favorite_Click(object sender, EventArgs e)
        {
            Favorite.Items.Add(txt_go.Text);
        }


private void btn_3_Click(object sender, EventArgs e)
        {
            History.Items.Clear();
        }



No comments:

Post a Comment